android 创建快捷方式的两种方式+判断是否已经创建+删除快捷方式

1.   在清单文件里面进行注册:例如:

[java]  view plain copy
  1. <activity  
  2.            android:name="com.android.master.legend.widget.CreateSystemSettingsWidgetActivity"  
  3.            android:exported="true"  
  4.            android:icon="@drawable/ic_switcher_shortcut"  
  5.            android:label="@string/system_switcher_shortcut"  
  6.            android:screenOrientation="portrait"  
  7.            android:theme="@android:style/Theme.Translucent.NoTitleBar" >  
  8.            <intent-filter>  
  9.                <action android:name="android.intent.action.CREATE_SHORTCUT" />  
  10.                <category android:name="android.intent.category.DEFAULT" />  
  11.            </intent-filter>  
  12.        </activity>  
这样,就会自动加入到 系统launcher的快捷方式里面

2.  手动创建快捷方式

[java]  view plain copy
  1. public static void createSystemSwitcherShortCut(Context context) {  
  2.         final Intent addIntent = new Intent(  
  3.                 "com.android.launcher.action.INSTALL_SHORTCUT");  
  4.         final Parcelable icon = Intent.ShortcutIconResource.fromContext(  
  5.                 context, R.drawable.ic_switcher_shortcut); // 获取快捷键的图标  
  6.         addIntent.putExtra("duplicate"false);  
  7.         final Intent myIntent = new Intent(context,  
  8.                 SystemSwitcherActivity.class);  
  9.         myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  10.         addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,  
  11.                 context.getString(R.string.switch_widget));// 快捷方式的标题  
  12.         addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);// 快捷方式的图标  
  13.         addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent);// 快捷方式的动作  
  14.         context.sendBroadcast(addIntent);  
  15.     }  
更具有通用性的写法如下:
[java]  view plain copy
  1. /**  
  2.      * 为程序创建桌面快捷方式  
  3.      */   
  4.     private void addShortcut(){    
  5.         Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");    
  6.                  
  7.         //快捷方式的名称    
  8.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));    
  9.         shortcut.putExtra("duplicate"false); //不允许重复创建    
  10.    
  11.         /****************************此方法已失效*************************/  
  12.         //ComponentName comp = new ComponentName(this.getPackageName(), "."+this.getLocalClassName());    
  13.         //shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));      
  14.      /******************************end*******************************/  
  15.      Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);  
  16.      shortcutIntent.setClassName(thisthis.getClass().getName());  
  17.      shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);  
  18.    
  19.         //快捷方式的图标    
  20.         ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);    
  21.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);    
  22.                  
  23.         sendBroadcast(shortcut);    
  24.     }   


小结:一般做法是在设置里面加上手动创建快捷方式的设置。

            在程序第一次启动的时候,手动创建一次快捷方式。

3.判断是否已经创建了快捷方式(在某些机型中需要判断)

[java]  view plain copy
  1. private boolean hasShortcut()  
  2. {  
  3.         boolean isInstallShortcut = false;  
  4.         final ContentResolver cr = activity.getContentResolver();  
  5.         final String AUTHORITY ="com.android.launcher.settings";  
  6.         final Uri CONTENT_URI = Uri.parse("content://" +AUTHORITY + "/favorites?notify=true");  
  7.         Cursor c = cr.query(CONTENT_URI,new String[] {"title","iconResource" },"title=?",  
  8.         new String[] {mapViewActivity.getString(R.string.app_name).trim()}, null);  
  9.         if(c!=null && c.getCount()>0){  
  10.             isInstallShortcut = true ;  
  11.         }  
  12.         return isInstallShortcut ;  

4.删除

[java]  view plain copy
  1. /**  
  2.      * 删除程序的快捷方式  
  3.      */   
  4.     private void delShortcut(){    
  5.         Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");    
  6.                  
  7.         //快捷方式的名称    
  8.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));    
  9.         String appClass = this.getPackageName() + "." +this.getLocalClassName();    
  10.         ComponentName comp = new ComponentName(this.getPackageName(), appClass);    
  11.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));    
  12.                  
  13.         sendBroadcast(shortcut);    
  14.                  
  15.     }    

5.声明权限

在AndroidManifest.xml 文件中声明 创建和删除快捷方式时声明权限
[html]  view plain copy
  1. <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />    
  2. <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />  


6.一个应用开启多个的问题

现在都很流行在桌面上为自己的应用创建快捷方式,网上也很多例子,但是基本都是同一个,创建快捷方式的手法是对的,但是通过快捷方式开启自己的应用的时候会发现程序菜单里头打开的应用和桌面快捷方式打开的应用竟然不是同一个,这样会导致一个应用开启了多个。
 
一开始从activity的加载模式入手,把默认的activity的加载模式设置成了android:launchMode="singleInstance"   虽然每次都只开启了同一个页面,但是每次都是从新加载的,而不是直接调到已经开启的activity,这个交互很不理想;
 
经过仔细研究log发现,通过桌面快捷方式开启应用和通过程序菜单开启应用的日志信息差了一个东西cat=[android.intent.category.LAUNCHER],通过快捷方式开启应用并没有打印出这个属性,所以就尝试在快捷方式创建的时候给他的intent添加一个属性,
 
[java]  view plain copy
  1. ComponentName comp = new ComponentName(this.getPackageName(), this.getPackageName() + "." +this.getLocalClassName());    
  2.   Intent intent = new Intent(Intent.ACTION_MAIN).setComponent(comp);  
  3.   intent.addCategory(Intent.CATEGORY_LAUNCHER);  
  4.   shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);  

 
其中,关键的就是intent.addCategory(Intent.CATEGORY_LAUNCHER);  完美解决了标题提到的问题!
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值