国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android O添加桌面快捷方式的示例

2019-12-12 01:18:01
字體:
來源:轉載
供稿:網友

手機升級到安卓O后,突然發現創建快捷方式的功能失效了,查詢一番后發現:安卓O要使用ShortcutManager來創建快捷方式。

安卓N及以下版本:

Intent addShortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");//"com.android.launcher.action.INSTALL_SHORTCUT"http:// 不允許重復創建addShortcutIntent.putExtra("duplicate", false);// 經測試不是根據快捷方式的名字判斷重復的// 應該是根據快鏈的Intent來判斷是否重復的,即Intent.EXTRA_SHORTCUT_INTENT字段的value// 但是名稱不同時,雖然有的手機系統會顯示Toast提示重復,仍然會建立快鏈// 屏幕上沒有空間時會提示// 注意:重復創建的行為MIUI和三星手機上不太一樣,小米上似乎不能重復創建快捷方式// 名字addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "網絡設置");// 圖標addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,  Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_perm_data_setting_black_24dp));// 設置關聯程序Intent launcherIntent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);//設置網絡頁面intent// 設置關聯程序// Intent launcherIntent = new Intent(Intent.ACTION_MAIN);// launcherIntent.setClass(MainActivity.this, MainActivity.class);// launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER);addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launcherIntent);// 發送廣播sendBroadcast(addShortcutIntent);

安卓O:

ShortcutManager scm = (ShortcutManager) getSystemService(SHORTCUT_SERVICE);Intent launcherIntent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);//設置網絡頁面intentShortcutInfo si = new ShortcutInfo.Builder(this, "dataroam")  .setIcon(Icon.createWithResource(this, R.drawable.ic_perm_data_setting_black_24dp))  .setShortLabel("網絡設置")  .setIntent(launcherIntent)  .build();assert scm != null;scm.requestPinShortcut(si, null);

那如果要兩者兼顧呢,則可以如下這樣寫:

//添加快捷方式private void addShortcut() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {  ShortcutManager scm = (ShortcutManager) getSystemService(SHORTCUT_SERVICE);  Intent launcherIntent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);//設置網絡頁面intent  ShortcutInfo si = new ShortcutInfo.Builder(this, "dataroam")    .setIcon(Icon.createWithResource(this, R.drawable.ic_perm_data_setting_black_24dp))    .setShortLabel("網絡設置")    .setIntent(launcherIntent)    .build();  assert scm != null;  scm.requestPinShortcut(si, null); } else {  Intent addShortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");//"com.android.launcher.action.INSTALL_SHORTCUT"  // 不允許重復創建  addShortcutIntent.putExtra("duplicate", false);// 經測試不是根據快捷方式的名字判斷重復的  // 應該是根據快鏈的Intent來判斷是否重復的,即Intent.EXTRA_SHORTCUT_INTENT字段的value  // 但是名稱不同時,雖然有的手機系統會顯示Toast提示重復,仍然會建立快鏈  // 屏幕上沒有空間時會提示  // 注意:重復創建的行為MIUI和三星手機上不太一樣,小米上似乎不能重復創建快捷方式  // 名字  addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "網絡設置");  // 圖標  addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,    Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_perm_data_setting_black_24dp));  // 設置關聯程序  Intent launcherIntent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);//設置網絡頁面intent  // 設置關聯程序//  Intent launcherIntent = new Intent(Intent.ACTION_MAIN);//  launcherIntent.setClass(MainActivity.this, MainActivity.class);//  launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER);  addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launcherIntent);  // 發送廣播  sendBroadcast(addShortcutIntent); }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 松阳县| 萨嘎县| 上犹县| 行唐县| 宜春市| 来凤县| 大悟县| 高阳县| 顺昌县| 灵山县| 察隅县| 怀集县| 盐边县| 凌云县| 潢川县| 松潘县| 衡南县| 榆中县| 潜江市| 商南县| 闵行区| 乐陵市| 清涧县| 昌黎县| 南郑县| 奇台县| 兰考县| 定安县| 宁远县| 嘉祥县| 黄石市| 永修县| 宁化县| 浦城县| 屯门区| 安远县| 烟台市| 固镇县| 灵宝市| 德昌县| 千阳县|