Android快捷方式是桌面最基本的組件,它可以用于直接啟動應(yīng)用程序的組件,下面是武林技術(shù)頻道和大家分享android應(yīng)用程序創(chuàng)建桌面快捷方式技巧,一起進(jìn)入下文了解一下吧!
分享android應(yīng)用程序創(chuàng)建桌面快捷方式技巧
復(fù)制代碼 代碼如下:
/**
* 創(chuàng)建桌面快捷方式
*/
private void createShortcut() {
SharedPreferences setting = getSharedPreferences("silent.preferences", 0);
// 判斷是否第一次啟動應(yīng)用程序(默認(rèn)為true)
boolean firstStart = setting.getBoolean("FIRST_START", true);
// 第一次啟動時創(chuàng)建桌面快捷方式
if (firstStart) {
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
// 快捷方式的名稱
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name2));
// 不允許重復(fù)創(chuàng)建
shortcut.putExtra("duplicate", false);
// 指定快捷方式的啟動對象
ComponentName comp = new ComponentName(this.getPackageName(), "." + this.getLocalClassName());
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));
// 快捷方式的圖標(biāo)
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.zhangxy);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
// 發(fā)出廣播
sendBroadcast(shortcut);
// 將第一次啟動的標(biāo)識設(shè)置為false
Editor editor = setting.edit();
editor.putBoolean("FIRST_START", false);
// 提交設(shè)置
editor.commit();
}
}
然后在onCreate()方法里加上上面方法名稱就行了:
?
復(fù)制代碼 代碼如下:
?
// 安裝后第一次啟動時創(chuàng)建桌面快捷方式
createShortcut();
?
上文是分享android應(yīng)用程序創(chuàng)建桌面快捷方式技巧,相信大家都有了一定的了解,想要了解更多的技術(shù)信息,請繼續(xù)關(guān)注武林技術(shù)頻道吧!