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


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

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

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

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

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

[java]  view plain copy
  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) {  
  127.             return null;  
  128.         }  
  129.         for (PackageInfo pack : packs) {  
  130.             ProviderInfo[] providers = pack.providers;  
  131.             if (providers != null) {  
  132.                 for (ProviderInfo provider : providers) {  
  133.                     if (permission.equals(provider.readPermission)|| permission.equals(provider.writePermission)) {  
  134.                         return provider.authority;  
  135.                     }  
  136.                 }  
  137.             }  
  138.         }  
  139.         return null;  
  140.     }  
  141.       
  142.       
  143.       
  144.     /** 
  145.      * 获取应用的名称 
  146.      * @param context 
  147.      * @return 
  148.      * @throws NameNotFoundException 
  149.      */  
  150.     private static String obtatinAppName(Context context) throws NameNotFoundException{  
  151.         PackageManager packageManager = context.getPackageManager();  
  152.         return packageManager.getApplicationLabel(packageManager.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA)).toString();  
  153.     }  
  154.   
  155. }  


接下来我们来使用该工具类,我们在onCreate()的方法中先判断桌面上是否有该快捷方式,没有我们就创建一个快捷方式,然后提供一个删除快捷方式的按钮,代码还是比较简单,相信你很容易看懂的

[java]  view plain copy
  1. package com.example.shortcut;  
  2.   
  3.   
  4. import android.app.Activity;  
  5. import android.os.Bundle;  
  6. import android.util.Log;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10.   
  11. public class MainActivity extends Activity implements OnClickListener{  
  12.     private final static String TAG = "Activity";  
  13.   
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.         Log.i(TAG, "onCreate");  
  19.           
  20.         if(! ShortCutUtils.hasShortcut(this)){  
  21.             ShortCutUtils.addShortCut(this, getString(R.string.app_name), R.drawable.icon);  
  22.         }  
  23.           
  24.         //删除快捷方式的按钮  
  25.         Button mButton = (Button) findViewById(R.id.delete);  
  26.         mButton.setOnClickListener(this);  
  27.           
  28.     }  
  29.   
  30.     @Override  
  31.     public void onClick(View v) {  
  32.         switch (v.getId()) {  
  33.         case R.id.delete:  
  34.             ShortCutUtils.delShortcut(this);  
  35.             break;  
  36.   
  37.         default:  
  38.             break;  
  39.         }  
  40.     }  
  41.   
  42. }  
这样子我们就添加好了快捷方法,可是你会发现

一、当我们进入MainActivity的时候,然后按HOME键进入后台,找到该桌面快捷方式点击,你会发现MainActivity的onCreate()被再一次的执行

二、你删掉我们添加的快捷方式,然后再应用主界面找到该应用图片,长按几秒钟,系统也会帮我们创建一个桌面快捷方式,你进入MainActivity,然后按HOME键,找到桌面快捷方式进入MainActivity,这时候你会发现,MainActivity的onCreate()方法没有被执行了显然第一种方式不是我们想要的,那怎么才能实现我们想要的第二种方式呢,别着急,我们只需要小小的修改一下,将上面的添加快捷方式Intent修改如下

[java]  view plain copy
  1. shortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent()  
  2.         .setAction(Intent.ACTION_MAIN)  
  3.         .addCategory(Intent.CATEGORY_LAUNCHER)  
  4.         .setClass(context, cls));    

设置好了,你在试一试,这时候你会发现,很应用列表长按的效果一样了,哈哈!

接下来我们来讨论下点击Notifycation的问题

我们点击一个按钮产生一个Notifycation,当我们点击Notifycation的时候,我们在onCreate()中调用如下方法来初始化Notifycation的有关东西

[java]  view plain copy
  1. /** 
  2.  * 初始化Notifycation 
  3.  */  
  4. private void initNotify(){  
  5.     nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
  6.     n = new Notification();  
  7.     n.flags = Notification.FLAG_AUTO_CANCEL;;  
  8.     n.icon = R.drawable.notification_icon;  
  9.     n.when = System.currentTimeMillis();  
  10.     n.flags = Notification.FLAG_AUTO_CANCEL;  
  11.     n.defaults = Notification.DEFAULT_SOUND;  
  12.     n.tickerText = "CSDN给你发来了一条消息,请查看!";  
  13.     Intent intent = new Intent().setClass(getApplication(), MainActivity.class);  
  14.     PendingIntent pi = PendingIntent.getActivity(this0, intent, 0);  
  15.     n.setLatestEventInfo(getApplication(), "我的微信""CSDN给你发来了一条消息,请查看!", pi);  
  16. }  
当我们产生通知的时候,点击通知进入MainActivity,此时的MainActivity并没有被销毁,我们发现MainActivity被重新创建了,这并不是我们想要的效果,可不可以做成如果Activity在栈中我们不重新创建,答案是肯定的,我们将上面的修改做类似的修改

[java]  view plain copy
  1. /** 
  2.  * 初始化Notifycation 
  3.  */  
  4. private void initNotify(){  
  5.     nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
  6.     n = new Notification();  
  7.     n.flags = Notification.FLAG_AUTO_CANCEL;;  
  8.     n.icon = R.drawable.notification_icon;  
  9.     n.when = System.currentTimeMillis();  
  10.     n.flags = Notification.FLAG_AUTO_CANCEL;  
  11.     n.defaults = Notification.DEFAULT_SOUND;  
  12.     n.tickerText = "CSDN给你发来了一条消息,请查看!";  
  13.     Intent intent = new Intent()  
  14.     .setAction(Intent.ACTION_MAIN)  
  15.     .addCategory(Intent.CATEGORY_LAUNCHER)  
  16.     .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)  
  17.     .setClass(getApplication(), MainActivity.class);  
  18.     PendingIntent pi = PendingIntent.getActivity(this0, intent, 0);  
  19.     n.setLatestEventInfo(getApplication(), "我的微信""CSDN给你发来了一条消息,请查看!", pi);  
  20. }  

问题就解决了,这个类似扣扣的效果,你点击Notifycation跳转到处于栈顶的Activity,这样是不是很方便呢,如果你觉得这篇文章对你有点帮助你就顶下,如果你发现错误请指出,谢谢!

上面那个创建快捷方式的工具类有点错误,不能删除创建的快捷方式,我将修改好的工具类贴在下面,也是完整的代码,直接可以用的

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.example.shortcut;  
  2.   
  3. import java.util.List;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.Context;  
  7. import android.content.Intent;  
  8. import android.content.Intent.ShortcutIconResource;  
  9. import android.content.pm.PackageInfo;  
  10. import android.content.pm.PackageManager;  
  11. import android.content.pm.PackageManager.NameNotFoundException;  
  12. import android.content.pm.ProviderInfo;  
  13. import android.database.Cursor;  
  14. import android.net.Uri;  
  15. import android.text.TextUtils;  
  16.   
  17. /** 
  18.  * 桌面快捷方式有关的工具类 
  19.  * @author xiaanming 
  20.  * 
  21.  */  
  22. public class ShortCutUtils {  
  23.     /** 
  24.      * 快捷方式添加的action 
  25.      */  
  26.     private final static String SHORTCUT_ADD_ACTION = "com.android.launcher.action.INSTALL_SHORTCUT";  
  27.     /** 
  28.      * 快捷方式删除的action 
  29.      */  
  30.     private final static String SHORTCUT_DEL_ACTION = "com.android.launcher.action.UNINSTALL_SHORTCUT";  
  31.     /** 
  32.      * 读取数据库需要的权限 
  33.      */  
  34.     private final static String READ_SETTINGS_PERMISSION = "com.android.launcher.permission.READ_SETTINGS";  
  35.   
  36.     /** 
  37.      * 添加快捷方式到桌面,添加快捷方式需要添加用户权限 
  38.      * <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />  
  39.      * @param context      当前的context对象 
  40.      * @param resourceId    快捷方式的图标资源id 
  41.      */  
  42.     public static void addShortCut(Context context, int resourceId){  
  43.         Intent shortCutIntent = new Intent(SHORTCUT_ADD_ACTION);  
  44.         //添加快捷方式的名字  
  45.          // 获取当前应用名称  
  46.         String appName = null;  
  47.         try {  
  48.             appName = obtatinAppName(context);  
  49.         } catch (NameNotFoundException e) {  
  50.             e.printStackTrace();  
  51.         }  
  52.         shortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName);  
  53.         //不允许重复添加  
  54.         shortCutIntent.putExtra("duplicate"false);  
  55.           
  56.         shortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN)  
  57.         .addCategory(Intent.CATEGORY_LAUNCHER).setClassName(context.getPackageName(), context.getClass().getName()));  
  58.         //添加快捷方式的图标  
  59.         ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(context, resourceId);      
  60.         shortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);      
  61.                    
  62.         context.sendBroadcast(shortCutIntent);      
  63.     }  
  64.       
  65.       
  66.     /** 
  67.      * 删除桌面上的快捷方式,需要添加权限 
  68.      * <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" /> 
  69.      * @param context 
  70.      */  
  71.     public static void delShortcut(Context context, Activity activity) {  
  72.         Intent shortcut = new Intent(SHORTCUT_DEL_ACTION);  
  73.         // 获取当前应用名称  
  74.         String appName = null;  
  75.         try {  
  76.             appName = obtatinAppName(context);  
  77.         } catch (NameNotFoundException e) {  
  78.             e.printStackTrace();  
  79.         }  
  80.         // 快捷方式名称  
  81.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName);  
  82.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN)  
  83.         .addCategory(Intent.CATEGORY_LAUNCHER).setClassName(context.getPackageName(), context.getClass().getName()));  
  84.         context.sendBroadcast(shortcut);  
  85.     }  
  86.       
  87.     /** 
  88.      * 判断桌面上是否有快捷方式,调用此方法需要添加权限 
  89.      * <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" /> 
  90.      * @param context 
  91.      * @return 
  92.      * @throws NameNotFoundException 
  93.      */  
  94.     public static boolean hasShortcut(Context context) {  
  95.         String AUTHORITY = getAuthorityFromPermission(context, READ_SETTINGS_PERMISSION);  
  96.           
  97.         System.out.println(AUTHORITY);  
  98.           
  99.         if (AUTHORITY == null) {  
  100.             return false;  
  101.         }  
  102.         Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true");  
  103.         String appName = null;  
  104.         try {  
  105.             appName = obtatinAppName(context);  
  106.         } catch (NameNotFoundException e) {  
  107.             e.printStackTrace();  
  108.         }  
  109.         Cursor c = context.getContentResolver().query(CONTENT_URI, new String[] { "title" }, "title=?"new String[] { appName },null);  
  110.         if (c != null && c.getCount() > 0) {  
  111.             return true;  
  112.         }  
  113.         return false;  
  114.     }  
  115.       
  116.     /** 
  117.      * android系统桌面的基本信息由一个launcher.db的Sqlite数据库管理,里面有三张表 
  118.      * 其中一张表就是favorites。这个db文件一般放在data/data/com.android.launcher(launcher2)文件的databases下 
  119.      * 但是对于不同的rom会放在不同的地方 
  120.      * 例如MIUI放在data/data/com.miui.home/databases下面 
  121.      * htc放在data/data/com.htc.launcher/databases下面 
  122.      * @param context 
  123.      * @param permission  读取设置的权限  READ_SETTINGS_PERMISSION 
  124.      * @return 
  125.      */  
  126.     private static String getAuthorityFromPermission(Context context, String permission) {  
  127.         if (TextUtils.isEmpty(permission)) {  
  128.             return null;  
  129.         }  
  130.         List<PackageInfo> packs = context.getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS);  
  131.         if (packs == null) {  
  132.             return null;  
  133.         }  
  134.         for (PackageInfo pack : packs) {  
  135.             ProviderInfo[] providers = pack.providers;  
  136.             if (providers != null) {  
  137.                 for (ProviderInfo provider : providers) {  
  138.                     if (permission.equals(provider.readPermission)|| permission.equals(provider.writePermission)) {  
  139.                         return provider.authority;  
  140.                     }  
  141.                 }  
  142.             }  
  143.         }  
  144.         return null;  
  145.     }  
  146.       
  147.       
  148.       
  149.     /** 
  150.      * 获取应用的名称 
  151.      * @param context 
  152.      * @return 
  153.      * @throws NameNotFoundException 
  154.      */  
  155.     private static String obtatinAppName(Context context) throws NameNotFoundException{  
  156.         PackageManager packageManager = context.getPackageManager();  
  157.         return packageManager.getApplicationLabel(packageManager.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA)).toString();  
  158.     }  
  159.   
  160. }  

上面的有些代码不太完整代码下载


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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值