溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android O如何添加桌面快捷方式

發(fā)布時(shí)間:2021-07-26 13:46:13 來源:億速云 閱讀:145 作者:小新 欄目:移動(dòng)開發(fā)

這篇文章主要為大家展示了“Android O如何添加桌面快捷方式”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Android O如何添加桌面快捷方式”這篇文章吧。

手機(jī)升級(jí)到安卓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)測(cè)試不是根據(jù)快捷方式的名字判斷重復(fù)的
// 應(yīng)該是根據(jù)快鏈的Intent來判斷是否重復(fù)的,即Intent.EXTRA_SHORTCUT_INTENT字段的value
// 但是名稱不同時(shí),雖然有的手機(jī)系統(tǒng)會(huì)顯示Toast提示重復(fù),仍然會(huì)建立快鏈
// 屏幕上沒有空間時(shí)會(huì)提示
// 注意:重復(fù)創(chuàng)建的行為MIUI和三星手機(jī)上不太一樣,小米上似乎不能重復(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)測(cè)試不是根據(jù)快捷方式的名字判斷重復(fù)的
  // 應(yīng)該是根據(jù)快鏈的Intent來判斷是否重復(fù)的,即Intent.EXTRA_SHORTCUT_INTENT字段的value
  // 但是名稱不同時(shí),雖然有的手機(jī)系統(tǒng)會(huì)顯示Toast提示重復(fù),仍然會(huì)建立快鏈
  // 屏幕上沒有空間時(shí)會(huì)提示
  // 注意:重復(fù)創(chuàng)建的行為MIUI和三星手機(jī)上不太一樣,小米上似乎不能重復(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);
 }
}

以上是“Android O如何添加桌面快捷方式”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI