建立,刪除快捷圖示shortcut android .

yangxi_001發表於2016-03-23

在manifest.xml中,新增許可權:


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


程式碼如下:


  1. private void uninstallShortcut(){  
  2.      Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");    
  3.                                  //com.android.launcher.action.UNINSTALL_SHORTCUT   
  4.           
  5.         //快捷方式的名稱        
  6.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));       
  7.         shortcut.putExtra("duplicate"false);   
  8.         Intent intent = new Intent(Intent.ACTION_MAIN);  
  9.         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  10.         intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
  11.           
  12.         intent.setClass(getApplicationContext(), MainActivity.class);  
  13.          
  14.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent);       
  15.                    
  16.         sendBroadcast(shortcut);       
  17. }  
  18.   
  19. private void installShortcut(){  
  20.     Log.i("CreateShortcutActivity","onclick to create shortcut");  
  21.     Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");  
  22.     shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,getString(R.string.app_name));  
  23.     shortcutIntent.putExtra("duplicate"false);  
  24.     Intent intent = new Intent(Intent.ACTION_MAIN);  
  25.     intent.setClass(getApplicationContext(), MainActivity.class);  
  26.   
  27.     shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);  
  28.     shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,  
  29.             Intent.ShortcutIconResource.fromContext(CreateShortcutActivity.this,  
  30.                     R.drawable.ic_shortcut));  
  31.     sendBroadcast(shortcutIntent);  
  32. }  
	private void uninstallShortcut(){
		 Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");  
		 							 //com.android.launcher.action.UNINSTALL_SHORTCUT
         
		    //快捷方式的名稱     
		    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));     
		    shortcut.putExtra("duplicate", false); 
		    Intent intent = new Intent(Intent.ACTION_MAIN);
		    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		    
		    intent.setClass(getApplicationContext(), MainActivity.class);
		   
		    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent);     
		             
		    sendBroadcast(shortcut);     
	}
	
	private void installShortcut(){
		Log.i("CreateShortcutActivity","onclick to create shortcut");
		Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
		shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,getString(R.string.app_name));
		shortcutIntent.putExtra("duplicate", false);
		Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.setClass(getApplicationContext(), MainActivity.class);

		shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
		shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
				Intent.ShortcutIconResource.fromContext(CreateShortcutActivity.this,
						R.drawable.ic_shortcut));
		sendBroadcast(shortcutIntent);
	}

在android中,有InstallShortcutReceiver,和UninstallShortcutReceiver兩個類,用來接收該廣播。


注意:shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);

在InstallShortcutReceiver中,如果intent沒有設定action的話,會新增Intent.ACTION_VIEW,造成在Remove shortcut的時候,比較兩個Intent時,可能不一致而無法刪除該shortcut.

所以,建議要自己新增ACTION.

附上android Intent.filterEquals().就是這方法用來比較前後兩個Intent的,


  1. /** 
  2.    * Determine if two intents are the same for the purposes of intent 
  3.    * resolution (filtering). That is, if their action, data, type, 
  4.    * class, and categories are the same.  This does <em>not</em> compare 
  5.    * any extra data included in the intents. 
  6.    * 
  7.    * @param other The other Intent to compare against. 
  8.    * 
  9.    * @return Returns true if action, data, type, class, and categories 
  10.    *         are the same. 
  11.    */  
  12.   public boolean filterEquals(Intent other) {  
  13.       if (other == null) {  
  14.           return false;  
  15.       }  
  16.       if (mAction != other.mAction) {  
  17.           if (mAction != null) {  
  18.               if (!mAction.equals(other.mAction)) {  
  19.                   return false;  
  20.               }  
  21.           } else {  
  22.               if (!other.mAction.equals(mAction)) {  
  23.                   return false;  
  24.               }  
  25.           }  
  26.       }  
  27.       if (mData != other.mData) {  
  28.           if (mData != null) {  
  29.               if (!mData.equals(other.mData)) {  
  30.                   return false;  
  31.               }  
  32.           } else {  
  33.               if (!other.mData.equals(mData)) {  
  34.                   return false;  
  35.               }  
  36.           }  
  37.       }  
  38.       if (mType != other.mType) {  
  39.           if (mType != null) {  
  40.               if (!mType.equals(other.mType)) {  
  41.                   return false;  
  42.               }  
  43.           } else {  
  44.               if (!other.mType.equals(mType)) {  
  45.                   return false;  
  46.               }  
  47.           }  
  48.       }  
  49.       if (mPackage != other.mPackage) {  
  50.           if (mPackage != null) {  
  51.               if (!mPackage.equals(other.mPackage)) {  
  52.                   return false;  
  53.               }  
  54.           } else {  
  55.               if (!other.mPackage.equals(mPackage)) {  
  56.                   return false;  
  57.               }  
  58.           }  
  59.       }  
  60.       if (mComponent != other.mComponent) {  
  61.           if (mComponent != null) {  
  62.               if (!mComponent.equals(other.mComponent)) {  
  63.                   return false;  
  64.               }  
  65.           } else {  
  66.               if (!other.mComponent.equals(mComponent)) {  
  67.                   return false;  
  68.               }  
  69.           }  
  70.       }  
  71.       if (mCategories != other.mCategories) {  
  72.           if (mCategories != null) {  
  73.               if (!mCategories.equals(other.mCategories)) {  
  74.                   return false;  
  75.               }  
  76.           } else {  
  77.               if (!other.mCategories.equals(mCategories)) {  
  78.                   return false;  
  79.               }  
  80.           }  
  81.       }  
  82.   
  83.       return true;  
  84.   }  
  /**
     * Determine if two intents are the same for the purposes of intent
     * resolution (filtering). That is, if their action, data, type,
     * class, and categories are the same.  This does <em>not</em> compare
     * any extra data included in the intents.
     *
     * @param other The other Intent to compare against.
     *
     * @return Returns true if action, data, type, class, and categories
     *         are the same.
     */
    public boolean filterEquals(Intent other) {
        if (other == null) {
            return false;
        }
        if (mAction != other.mAction) {
            if (mAction != null) {
                if (!mAction.equals(other.mAction)) {
                    return false;
                }
            } else {
                if (!other.mAction.equals(mAction)) {
                    return false;
                }
            }
        }
        if (mData != other.mData) {
            if (mData != null) {
                if (!mData.equals(other.mData)) {
                    return false;
                }
            } else {
                if (!other.mData.equals(mData)) {
                    return false;
                }
            }
        }
        if (mType != other.mType) {
            if (mType != null) {
                if (!mType.equals(other.mType)) {
                    return false;
                }
            } else {
                if (!other.mType.equals(mType)) {
                    return false;
                }
            }
        }
        if (mPackage != other.mPackage) {
            if (mPackage != null) {
                if (!mPackage.equals(other.mPackage)) {
                    return false;
                }
            } else {
                if (!other.mPackage.equals(mPackage)) {
                    return false;
                }
            }
        }
        if (mComponent != other.mComponent) {
            if (mComponent != null) {
                if (!mComponent.equals(other.mComponent)) {
                    return false;
                }
            } else {
                if (!other.mComponent.equals(mComponent)) {
                    return false;
                }
            }
        }
        if (mCategories != other.mCategories) {
            if (mCategories != null) {
                if (!mCategories.equals(other.mCategories)) {
                    return false;
                }
            } else {
                if (!other.mCategories.equals(mCategories)) {
                    return false;
                }
            }
        }

        return true;
    }

可見,flag屬性是不在比較之列的,可以任意新增 

相關文章