Android 應用程序的快捷方式

mmp7 9年前發布 | 1K 次閱讀 Java

快捷方式的添加與刪除都是通過廣播來實現

權限:

    <uses-permission  android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />  
    <uses-permission  android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />  

    //為程序創建桌面快捷方式  
    Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");  
    //快捷方式的名稱  
    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.ic_launcher);  
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);  
    sendBroadcast(shortcut);  

    //刪除快捷方式
Intent delIntent = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
//快捷方式的名稱
delIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));

//指定當前的Activity為快捷方式啟動的對象: 如 //com.test.app  
//注意: ComponentName的第二個參數必須是完整的類名(包名+類名),否則無法刪除快捷方式  
String appClass = this.getPackageName() + "." +this.getLocalClassName();  
ComponentName compn = new ComponentName(this.getPackageName(), appClass);  
delIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(compn));  
sendBroadcast(delIntent);  </pre> 


 本文由用戶 mmp7 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!