最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2018年01月16日 08:31:53   作者:Maxiye  
本篇文章主要介紹了AndroidO添加桌面快捷方式的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

手機(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);
 }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

壤塘县| 梓潼县| 开平市| 平阳县| 墨竹工卡县| 苗栗县| 涪陵区| 礼泉县| 平泉县| 安吉县| 宁武县| 鸡东县| 虹口区| 鹿邑县| 兴宁市| 江川县| 拉孜县| 赤水市| 盐池县| 焦作市| 肥东县| 建阳市| 鄂尔多斯市| 宿州市| 突泉县| 靖州| 六安市| 三河市| 克拉玛依市| 磐安县| 东乌| 平阳县| 临桂县| 哈尔滨市| 安国市| 万州区| 赣榆县| 宣威市| 苍南县| 长岛县| 隆子县|