android 建立 刪除桌面快捷方式

建立 /**android

  • 爲程序建立桌面快捷方式 */ private void addShortcut(){ Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");app

    //快捷方式的名稱 shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name)); shortcut.putExtra("duplicate", false); //不容許重複建立ide

    //指定當前的Activity爲快捷方式啓動的對象: 如 com.everest.video.VideoPlayer //注意: ComponentName的第二個參數必須加上點號(.),不然快捷方式沒法啓動相應程序 ComponentName comp = new ComponentName(this.getPackageName(), "."+this.getLocalClassName()); shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp)); //快捷方式的圖標 ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon); shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);this

    sendBroadcast(shortcut); }/**rest

  • 爲程序建立桌面快捷方式 */ private void addShortcut(){ Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");xml

//快捷方式的名稱 shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name)); shortcut.putExtra("duplicate", false); //不容許重複建立對象

//指定當前的Activity爲快捷方式啓動的對象: 如 com.everest.video.VideoPlayer //注意: ComponentName的第二個參數必須加上點號(.),不然快捷方式沒法啓動相應程序 ComponentName comp = new ComponentName(this.getPackageName(), "."+this.getLocalClassName()); shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp)); //快捷方式的圖標 ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon); shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);get

sendBroadcast(shortcut); }string

2, 刪除 /**it

  • 刪除程序的快捷方式 */ private void delShortcut(){ Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");

    //快捷方式的名稱 shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));

    //指定當前的Activity爲快捷方式啓動的對象: 如 com.everest.video.VideoPlayer //注意: ComponentName的第二個參數必須是完整的類名(包名+類名),不然沒法刪除快捷方式 String appClass = this.getPackageName() + "." +this.getLocalClassName(); ComponentName comp = new ComponentName(this.getPackageName(), appClass); shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));

    sendBroadcast(shortcut);

}/**

  • 刪除程序的快捷方式 */ private void delShortcut(){ Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");

//快捷方式的名稱 shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));

//指定當前的Activity爲快捷方式啓動的對象: 如 com.everest.video.VideoPlayer //注意: ComponentName的第二個參數必須是完整的類名(包名+類名),不然沒法刪除快捷方式 String appClass = this.getPackageName() + "." +this.getLocalClassName(); ComponentName comp = new ComponentName(this.getPackageName(), appClass); shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));

sendBroadcast(shortcut);

} 3, 聲明權限 在AndroidManifest.xml 文件中聲明 建立和刪除快捷方式時聲明權限 Java代碼 <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" /> <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" /> Android

相關文章
相關標籤/搜索