Android 点击桌面快捷方式和Notifycation跳转到Task栈顶Activity

               

转载时请注明出处:http://blog.csdn.net/xiaanming/article/details/9314193

我们一般下载的应用在第一次启动应用的时候都会给我创建一个桌面快捷方式,然后我在网上找了些资料整理下了,写了一个快捷方式的工具类,这样我们以后要创建快捷方式的时候直接拷贝这个类,里面提供了一些静态方法,主要的三个方法如下

1.addShortCut(Context context, String shortCutName, int resourceId, Class<?> cls)添加快捷方式的方法

2.delShortcut(Context context) 删除快捷方式的方法

3.hasShortcut(Context context)判断桌面上是否有该快捷方式的方法

工具类代码如下,使用这三个方法都需要添加相对于的权限,我在代码中也写的比较清楚

[java] view plaincopy
  1. package com.example.shortcut;  
  2.   
  3. import java.util.List;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.ComponentName;  
  7. import android.content.Context;  
  8. import android.content.Intent;  
  9. import android.content.Intent.ShortcutIconResource;  
  10. import android.content.pm.PackageInfo;  
  11. import android.content.pm.PackageManager;  
  12. import android.content.pm.ProviderInfo;  
  13. import android.content.pm.PackageManager.NameNotFoundException;  
  14. import android.database.Cursor;  
  15. import android.net.Uri;  
  16. import android.text.TextUtils;  
  17.   
  18. /** 
  19.  * 桌面快捷方式有关的工具类 
  20.  * @author xiaanming 
  21.  * 
  22.  */  
  23. public class ShortCutUtils {  
  24.     /** 
  25.      * 快捷方式添加的action 
  26.      */  
  27.     private final static String SHORTCUT_ADD_ACTION = "com.android.launcher.action.INSTALL_SHORTCUT";  
  28.     /** 
  29.      * 快捷方式删除的action 
  30.      */  
  31.     private final static String SHORTCUT_DEL_ACTION = "com.android.launcher.action.UNINSTALL_SHORTCUT";  
  32.     /** 
  33.      * 读取数据库需要的权限 
  34.      */  
  35.     private final static String READ_SETTINGS_PERMISSION = "com.android.launcher.permission.READ_SETTINGS";  
  36.       
  37.   
  38.     /** 
  39.      * 添加快捷方式到桌面,添加快捷方式需要添加用户权限 
  40.      * <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> 
  41.      * @param context 
  42.      * @param shortCutName 
  43.      * @param resourceId 
  44.      * @param cls 
  45.      */  
  46.     public static void addShortCut(Context context, String shortCutName, int resourceId, Class<?> cls){  
  47.         Intent shortCutIntent = new Intent(SHORTCUT_ADD_ACTION);  
  48.         //添加快捷方式的名字  
  49.         shortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortCutName);  
  50.         //不允许重复添加  
  51.         shortCutIntent.putExtra("duplicate"false);  
  52.           
  53.         //指定当前的Activity为快捷方式启动的对象    
  54.         shortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent().setClass(context, cls));      
  55.           
  56.         //添加快捷方式的图标  
  57.         ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(context, resourceId);      
  58.         shortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);      
  59.                    
  60.         context.sendBroadcast(shortCutIntent);      
  61.     }  
  62.       
  63.       
  64.     /** 
  65.      * 删除桌面上的快捷方式,需要添加权限 
  66.      * <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" /> 
  67.      * @param context 
  68.      */  
  69.     public static void delShortcut(Context context) {  
  70.         Intent shortcut = new Intent(SHORTCUT_DEL_ACTION);  
  71.         // 获取当前应用名称  
  72.         String appName = null;  
  73.         try {  
  74.             appName = obtatinAppName(context);  
  75.         } catch (NameNotFoundException e) {  
  76.             e.printStackTrace();  
  77.         }  
  78.         // 快捷方式名称  
  79.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName);  
  80.         Intent shortcutIntent = context.getPackageManager() .getLaunchIntentForPackage(context.getPackageName());  
  81.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);  
  82.         context.sendBroadcast(shortcut);  
  83.     }  
  84.       
  85.     /** 
  86.      * 判断桌面上是否有快捷方式,调用此方法需要添加权限 
  87.      * <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" /> 
  88.      * @param context 
  89.      * @return 
  90.      * @throws NameNotFoundException 
  91.      */  
  92.     public static boolean hasShortcut(Context context) {  
  93.         String AUTHORITY = getAuthorityFromPermission(context, READ_SETTINGS_PERMISSION);  
  94.         if (AUTHORITY == null) {  
  95.             return false;  
  96.         }  
  97.         Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true");  
  98.         String appName = null;  
  99.         try {  
  100.             appName = obtatinAppName(context);  
  101.         } catch (NameNotFoundException e) {  
  102.             e.printStackTrace();  
  103.         }  
  104.         Cursor c = context.getContentResolver().query(CONTENT_URI, new String[] { "title" }, "title=?"new String[] { appName },null);  
  105.         if (c != null && c.getCount() > 0) {  
  106.             return true;  
  107.         }  
  108.         return false;  
  109.     }  
  110.       
  111.     /** 
  112.      * android系统桌面的基本信息由一个launcher.db的Sqlite数据库管理,里面有三张表 
  113.      * 其中一张表就是favorites。这个db文件一般放在data/data/com.android.launcher(launcher2)文件的databases下 
  114.      * 但是对于不同的rom会放在不同的地方 
  115.      * 例如MIUI放在data/data/com.miui.home/databases下面 
  116.      * htc放在data/data/com.htc.launcher/databases下面 
  117.      * @param context 
  118.      * @param permission  读取设置的权限  READ_SETTINGS_PERMISSION 
  119.      * @return 
  120.      */  
  121.     private static String getAuthorityFromPermission(Context context, String permission) {  
  122.         if (TextUtils.isEmpty(permission)) {  
  123.             return null;  
  124.         }  
  125.         List<PackageInfo> packs = context.getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS);  
  126.         if (packs == null) {  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值