Android 添加和删除桌面快捷方式

原文 url http://blog.csdn.net/jjmm2009/article/details/37902949


为应用创建快捷方式目前有两种方法:

1. 程序启动时主动添加快捷方式到桌面------------>主动添加

2.长按桌面,弹出应用选择窗,拖动应用到桌面---------->被动添加


公用方法:

[java]  view plain copy
  1. /** 
  2.  
  3.       * 返回添加到桌面快捷方式的Intent:   
  4.  
  5.       * 1.给Intent指定action="com.android.launcher.INSTALL_SHORTCUT" 
  6.  
  7.       * 2.给定义为Intent.EXTRA_SHORTCUT_INENT的Intent设置与安装时一致的action(必须要有)   
  8.  
  9.       * 3.添加权限:com.android.launcher.permission.INSTALL_SHORTCUT 
  10.  
  11.       */  
  12.   
  13.      public static Intent getShortcutToDesktopIntent(Context context) {  
  14.          Intent intent = new Intent();   
  15.          intent.setClass(context, context.getClass());    
  16.         /*以下两句是为了在卸载应用的时候同时删除桌面快捷方式*/  
  17.          intent.setAction("android.intent.action.MAIN");    
  18.          intent.addCategory("android.intent.category.LAUNCHER");    
  19.           
  20.          Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");  
  21.          // 不允许重建  
  22.          shortcut.putExtra("duplicate"false);  
  23.          // 设置名字  
  24.          shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,context.getString(R.string.app_name));  
  25.          // 设置图标  
  26.          shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_launcher));  
  27.          // 设置意图和快捷方式关联程序  
  28.          shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent);  
  29.   
  30.          return shortcut;  
  31.   
  32.      }  


一、主动添加方式:

 1. 在AndroidManifest.xml中添加权限:

[java]  view plain copy
  1. <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>  

2. 在启动Activity中发送广播:

[java]  view plain copy
  1. sendBroadcast(getShortcutToDesktopIntent(MainActivity.this));  


二、被动添加方式:

1.在AndroidManifest.xml中添加权限:

[java]  view plain copy
  1. <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>  

2.在AndroidManifest.xml中为主Activity添加action监听:

[java]  view plain copy
  1. <!-- 如果是通过桌面长按添加快捷方式,才需要添加此配置 -->  
  2.             <intent-filter>    
  3.               <action android:name="android.intent.action.CREATE_SHORTCUT" />    
  4.               <category android:name="android.intent.category.DEFAULT" />    
  5.           </intent-filter>    

3.在启动Activity中添加广播监听:

[java]  view plain copy
  1. final Intent launchIntent = getIntent();  
  2.         final String action = launchIntent.getAction();  
  3.         if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {  
  4.             Log.i(TAG, "create shortcut method one---------------- ");  
  5.             setResult(RESULT_OK, ShortcutUtils.getShortcutToDesktopIntent(MainActivity.this));  
  6.               
  7.             finish();  
  8.         }   

三、删除快捷方式:

1.在AndroidManifest.xml中添加权限:

[java]  view plain copy
  1. <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>  

2.代码:

[java]  view plain copy
  1. /** 
  2.       * 删除快捷方式 
  3.       * */  
  4.      public static void deleteShortCut(Context context)  
  5.      {  
  6.         Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");    
  7.         //快捷方式的名称    
  8.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,context.getString(R.string.app_name));    
  9.         /**删除和创建需要对应才能找到快捷方式并成功删除**/  
  10.         Intent intent = new Intent();   
  11.         intent.setClass(context, context.getClass());    
  12.         intent.setAction("android.intent.action.MAIN");    
  13.         intent.addCategory("android.intent.category.LAUNCHER");    
  14.           
  15.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent);    
  16.         context.sendBroadcast(shortcut);            
  17.      }  


四、判断快捷方式是否已创建(该方法不起作用,方法中有说明和解决方案):

1.添加权限:

[java]  view plain copy
  1. <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>  

2.代码:

[java]  view plain copy
  1. /** 
  2.       * 判断是否已添加快捷方式:   
  3.       * 暂时没有方法能够准确的判断到快捷方式,原因是, 
  4.         1、不同厂商的机型他的快捷方式uri不同,我遇到过HTC的他的URI是content://com.htc.launcher.settings/favorites?notify=true 
  5.         2、桌面不只是android自带的,可能是第三方的桌面,他们的快捷方式uri都不同 
  6.          
  7.         提供一个解决办法,创建快捷方式的时候保存到preference,或者建个文件在SD卡上,下次加载的时候判断不存在就先发删除广播,再重新创建 
  8.  
  9.       * 添加权限:<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" ></uses-permission> 
  10.  
  11.       */  
  12.      public static boolean hasInstallShortcut(Context context) {  
  13.          boolean hasInstall = false;  
  14.   
  15.          String AUTHORITY = "com.android.launcher.settings";  
  16.          int systemversion = Build.VERSION.SDK_INT;  
  17.          Log.i("Build.VERSION.SDK==========>", systemversion + "");  
  18.          /*大于8的时候在com.android.launcher2.settings 里查询(未测试)*/  
  19.          if(systemversion >= 8){   
  20.              AUTHORITY = "com.android.launcher2.settings";   
  21.          }   
  22.          Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY  + "/favorites?notify=true");  
  23.   
  24.          Cursor cursor = context.getContentResolver().query(CONTENT_URI,  
  25.                  new String[] { "title" }, "title=?",  
  26.                  new String[] { context.getString(R.string.app_name) }, null);  
  27.   
  28.          if (cursor != null && cursor.getCount() > 0) {  
  29.              hasInstall = true;  
  30.          }  
  31.   
  32.          return hasInstall;  
  33.      }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值