摘要:手機升級到安卓后,突然發(fā)現(xiàn)創(chuàng)建快捷方式的功能失效了,查詢一番后發(fā)現(xiàn)安卓要使用來創(chuàng)建快捷方式。
手機升級到安卓O后,突然發(fā)現(xiàn)創(chuàng)建快捷方式的功能失效了,查詢一番后發(fā)現(xiàn):安卓O要使用ShortcutManager來創(chuàng)建快捷方式。
安卓N及以下版本:
Intent addShortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");//"com.android.launcher.action.INSTALL_SHORTCUT" // 不允許重復(fù)創(chuàng)建 addShortcutIntent.putExtra("duplicate", false);// 經(jīng)測試不是根據(jù)快捷方式的名字判斷重復(fù)的 // 應(yīng)該是根據(jù)快鏈的Intent來判斷是否重復(fù)的,即Intent.EXTRA_SHORTCUT_INTENT字段的value // 但是名稱不同時,雖然有的手機系統(tǒng)會顯示Toast提示重復(fù),仍然會建立快鏈 // 屏幕上沒有空間時會提示 // 注意:重復(fù)創(chuàng)建的行為MIUI和三星手機上不太一樣,小米上似乎不能重復(fù)創(chuàng)建快捷方式 // 名字 addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "網(wǎng)絡(luò)設(shè)置"); // 圖標(biāo) addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_perm_data_setting_black_24dp)); // 設(shè)置關(guān)聯(lián)程序 Intent launcherIntent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);//設(shè)置網(wǎng)絡(luò)頁面intent // 設(shè)置關(guān)聯(lián)程序 // 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); // 發(fā)送廣播 sendBroadcast(addShortcutIntent);
安卓O:
ShortcutManager scm = (ShortcutManager) getSystemService(SHORTCUT_SERVICE); Intent launcherIntent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);//設(shè)置網(wǎng)絡(luò)頁面intent ShortcutInfo si = new ShortcutInfo.Builder(this, "dataroam") .setIcon(Icon.createWithResource(this, R.drawable.ic_perm_data_setting_black_24dp)) .setShortLabel("網(wǎng)絡(luò)設(shè)置") .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);//設(shè)置網(wǎng)絡(luò)頁面intent ShortcutInfo si = new ShortcutInfo.Builder(this, "dataroam") .setIcon(Icon.createWithResource(this, R.drawable.ic_perm_data_setting_black_24dp)) .setShortLabel("網(wǎng)絡(luò)設(shè)置") .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" // 不允許重復(fù)創(chuàng)建 addShortcutIntent.putExtra("duplicate", false);// 經(jīng)測試不是根據(jù)快捷方式的名字判斷重復(fù)的 // 應(yīng)該是根據(jù)快鏈的Intent來判斷是否重復(fù)的,即Intent.EXTRA_SHORTCUT_INTENT字段的value // 但是名稱不同時,雖然有的手機系統(tǒng)會顯示Toast提示重復(fù),仍然會建立快鏈 // 屏幕上沒有空間時會提示 // 注意:重復(fù)創(chuàng)建的行為MIUI和三星手機上不太一樣,小米上似乎不能重復(fù)創(chuàng)建快捷方式 // 名字 addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "網(wǎng)絡(luò)設(shè)置"); // 圖標(biāo) addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_perm_data_setting_black_24dp)); // 設(shè)置關(guān)聯(lián)程序 Intent launcherIntent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);//設(shè)置網(wǎng)絡(luò)頁面intent // 設(shè)置關(guān)聯(lián)程序 // 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); // 發(fā)送廣播 sendBroadcast(addShortcutIntent); } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/68239.html
摘要:今天給手上的手機升級系統(tǒng)至體驗了一下新功能圖標(biāo)快捷方式如下圖所示如何實現(xiàn)這樣的快捷方式呢官方給出的實現(xiàn)步驟分類圖標(biāo)快捷方式分為兩種靜態(tài)快捷方式動態(tài)快捷方式靜態(tài)快捷方式是寫在文件中而動態(tài)快捷方式是在代碼中編寫實現(xiàn)環(huán)境要求只有及以上手機才能使用 今天給手上的Nexus6P手機升級系統(tǒng)至Android7.1,體驗了一下新功能:App Shortcuts(圖標(biāo)快捷方式),如下圖所示: show...
閱讀 2087·2021-11-16 11:45
閱讀 583·2021-11-04 16:12
閱讀 1390·2021-10-08 10:22
閱讀 864·2021-09-23 11:52
閱讀 4150·2021-09-22 15:47
閱讀 3527·2021-09-22 15:07
閱讀 498·2021-09-03 10:28
閱讀 1743·2021-09-02 15:21