创建,删除快捷图标shortcut android .

来源:互联网 发布:python 指数函数 编辑:程序博客网 时间:2024/04/30 05:17

在manifest.xml中,添加权限:


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


代码如下:


[java] view plain copy print?
  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. }  

在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的,


[java] view plain copy print?
  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.   }  

可见,flag属性是不在比较之列的,可以任意添加 
0 0
原创粉丝点击