2025-02-17

我的應用程序會在第一次使用的時候在桌面創建一個快捷方式,我創建快捷方式的代碼是這樣的:我的應用程序會在第一次使用的時候在桌面創建一個快捷方式,我創建快捷方式的代碼是這樣的:我的應用程序會在第一次使用的時候在桌面創建一個快捷方式,我創建快捷方式的代碼是這樣的:我的程序在第一次使用的時候會在桌面創建一個快捷方式,代碼如下:


Intent intent = new Intent(); 
        intent.setClass(this, SplashActivity.class); 
         
        Intent addShortcut = new Intent(ACTION_ADD_SHORTCUT); 
        Parcelable icon = Intent.ShortcutIconResource.fromContext(this, 
                R.drawable.icon); 
        addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name)); 
        addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent); 
        addShortcut.putExtra("duplicate", 0); 
        addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); 
        sendBroadcast(addShortcut); 
 

但是當程序被卸載掉之後卻沒有同時刪除桌面上的 快捷方式,這個問題昨天搞瞭我很久,今天終於解決瞭,方法是這樣的:

 Intent intent = new Intent(); 
        intent.setClass(this, SplashActivity.class); 
        intent.setAction("android.intent.action.MAIN"); 
        intent.addCategory("android.intent.category.LAUNCHER"); 
         
        Intent addShortcut = new Intent(ACTION_ADD_SHORTCUT); 
        Parcelable icon = Intent.ShortcutIconResource.fromContext(this, 
                R.drawable.icon); 
        addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name)); 
        addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent); 
        addShortcut.putExtra("duplicate", 0); 
        addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); 
        sendBroadcast(addShortcut); 

但是當程序被卸載掉之後卻沒有同時刪除桌面上的 快捷方式,這個問題昨天搞瞭我很久,今天終於解決瞭,方法是這樣的:也就是跟Intent增加瞭兩個過濾的屬性

intent.setAction("android.intent.action.MAIN");

intent.addCategory("android.intent.category.LAUNCHER");

這樣的話桌面圖標就會和我的應用綁定瞭,卸載瞭應用後系統也會同時自動刪除圖標。

摘自:yinkai1205的專欄

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *