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

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

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

[java] 
view plain
copy

 

  1. package com.example.shortcut;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. public class MainActivity extends Activity implements OnClickListener{
  9. private final static String TAG = "Activity";
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. Log.i(TAG, "onCreate");
  15. if(! ShortCutUtils.hasShortcut(this)){
  16. ShortCutUtils.addShortCut(this, getString(R.string.app_name), R.drawable.icon);
  17. }
  18. //删除快捷方式的按钮
  19. Button mButton = (Button) findViewById(R.id.delete);
  20. mButton.setOnClickListener(this);
  21. }
  22. @Override
  23. public void onClick(View v) {
  24. switch (v.getId()) {
  25. case R.id.delete:
  26. ShortCutUtils.delShortcut(this);
  27. break;
  28. default:
  29. break;
  30. }
  31. }
  32. }

这样子我们就添加好了快捷方法,可是你会发现

一、当我们进入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(this, 0, 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(this, 0, intent, 0);
  19. n.setLatestEventInfo(getApplication(), "我的微信", "CSDN给你发来了一条消息,请查看!", pi);
  20. }

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

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

package com.example.shortcut;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.Intent.ShortcutIconResource;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ProviderInfo;
import android.database.Cursor;
import android.net.Uri;
import android.text.TextUtils; /**
* 桌面快捷方式有关的工具类
* @author xiaanming
*
*/
public class ShortCutUtils {
/**
* 快捷方式添加的action
*/
private final static String SHORTCUT_ADD_ACTION = "com.android.launcher.action.INSTALL_SHORTCUT";
/**
* 快捷方式删除的action
*/
private final static String SHORTCUT_DEL_ACTION = "com.android.launcher.action.UNINSTALL_SHORTCUT";
/**
* 读取数据库需要的权限
*/
private final static String READ_SETTINGS_PERMISSION = "com.android.launcher.permission.READ_SETTINGS"; /**
* 添加快捷方式到桌面,添加快捷方式需要添加用户权限
* <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
* @param context 当前的context对象
* @param resourceId 快捷方式的图标资源id
*/
public static void addShortCut(Context context, int resourceId){
Intent shortCutIntent = new Intent(SHORTCUT_ADD_ACTION);
//添加快捷方式的名字
// 获取当前应用名称
String appName = null;
try {
appName = obtatinAppName(context);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
shortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName);
//不允许重复添加
shortCutIntent.putExtra("duplicate", false); shortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN)
.addCategory(Intent.CATEGORY_LAUNCHER).setClassName(context.getPackageName(), context.getClass().getName()));
//添加快捷方式的图标
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(context, resourceId);
shortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes); context.sendBroadcast(shortCutIntent);
} /**
* 删除桌面上的快捷方式,需要添加权限
* <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
* @param context
*/
public static void delShortcut(Context context, Activity activity) {
Intent shortcut = new Intent(SHORTCUT_DEL_ACTION);
// 获取当前应用名称
String appName = null;
try {
appName = obtatinAppName(context);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
// 快捷方式名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN)
.addCategory(Intent.CATEGORY_LAUNCHER).setClassName(context.getPackageName(), context.getClass().getName()));
context.sendBroadcast(shortcut);
} /**
* 判断桌面上是否有快捷方式,调用此方法需要添加权限
* <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
* @param context
* @return
* @throws NameNotFoundException
*/
public static boolean hasShortcut(Context context) {
String AUTHORITY = getAuthorityFromPermission(context, READ_SETTINGS_PERMISSION); System.out.println(AUTHORITY); if (AUTHORITY == null) {
return false;
}
Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true");
String appName = null;
try {
appName = obtatinAppName(context);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
Cursor c = context.getContentResolver().query(CONTENT_URI, new String[] { "title" }, "title=?", new String[] { appName },null);
if (c != null && c.getCount() > 0) {
return true;
}
return false;
} /**
* android系统桌面的基本信息由一个launcher.db的Sqlite数据库管理,里面有三张表
* 其中一张表就是favorites。这个db文件一般放在data/data/com.android.launcher(launcher2)文件的databases下
* 但是对于不同的rom会放在不同的地方
* 例如MIUI放在data/data/com.miui.home/databases下面
* htc放在data/data/com.htc.launcher/databases下面
* @param context
* @param permission 读取设置的权限 READ_SETTINGS_PERMISSION
* @return
*/
private static String getAuthorityFromPermission(Context context, String permission) {
if (TextUtils.isEmpty(permission)) {
return null;
}
List<PackageInfo> packs = context.getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS);
if (packs == null) {
return null;
}
for (PackageInfo pack : packs) {
ProviderInfo[] providers = pack.providers;
if (providers != null) {
for (ProviderInfo provider : providers) {
if (permission.equals(provider.readPermission)|| permission.equals(provider.writePermission)) {
return provider.authority;
}
}
}
}
return null;
} /**
* 获取应用的名称
* @param context
* @return
* @throws NameNotFoundException
*/
private static String obtatinAppName(Context context) throws NameNotFoundException{
PackageManager packageManager = context.getPackageManager();
return packageManager.getApplicationLabel(packageManager.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA)).toString();
} }

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

Android 点击桌面快捷方式和Notifycation跳转到Task栈顶Activity的更多相关文章

  1. android Activity启动过程(三)从栈顶Activity的onPause到启动activityon的Resume过程

    ActivityStack.startPausingLocked() IApplicationThread.schudulePauseActivity() ActivityThread.sendMes ...

  2. android 点击桌面图标,打开手机浏览器进入对应的站点

    做一个假的adnroid app.要实现点击桌面图标.打开手机浏览器进入对应的站点,实现方法非常easy import android.app.Activity; import android.con ...

  3. Android中的桌面快捷方式

    一.判断是否已有快捷方式 private String getAuthorityFromPermission(Context context, String permission){ if (perm ...

  4. android点击桌面App图标activity启动流程

    1.点击桌面App图标,Launcher进程采用Binder IPC向system_server进程发起startActivity请求:2.system_server进程接收到请求后,向zygote进 ...

  5. android Activity启动过程(二)从ActivityManagerService的startActivity到栈顶Activity的onPause过程

    ActivityManagerService.startActivity() ActvityiManagerService.startActivityAsUser() ActivityStackSup ...

  6. android桌面快捷方式跳转到指定activity

    AndroidManifest.xml 应用主入口配置: <activity android:name="com.*.cust.contacts.MainActivity" ...

  7. Android桌面快捷方式那些事与那些坑

    原文来自http://blog.zanlabs.com/2015/03/14/android-shortcut-summary/ 将近二个多月没写博客了.之前一段时间一直在搞红包助手,就没抽时间写博客 ...

  8. Android创建桌面快捷方式

    在桌面上创建特定界面的快捷入口,icon和title根据请求参数命名.在网上收集的一些相关资 料,在使用intent发送广播的时候,一些型号的收集会有问题,如魅族MX,红米,以及华为,使用setCla ...

  9. (转)Android创建桌面快捷方式两种方法

    [IT168技术]Android在桌面上生成快捷方式有两种情况,一种是直接在桌面直接生成;一种是长按桌面,在弹出的快捷菜单中生成. 谈谈在桌面上直接生成.个人觉得这个比较爽快,既然都是快捷方式了干嘛还 ...

随机推荐

  1. Python什么是值或引用函数参数

    这篇文章是Python前往遇到有疑问的功能. 下面一段是原有的基础教程Python函数. 按值传递參数和按引用传递參数 全部參数(自变量)在Python里都是按引用传递.假设你在函数里改动了參数,那么 ...

  2. SQL 2005 中查询或执行另外的数据库操作的方法

    原文:SQL 2005 中查询或执行另外的数据库操作的方法 摘要: 如果,你想在一台数据库服务器上,查询另一个台数据服务器的数据该如何做呢?如果,你想在同一台数据服务器上,在不同的数据库之间查询数据, ...

  3. c#开发之多国语言解决方案gnu.gettext + poedit

    1.工具简介 1.1.关于i18n i18n其来源是英文单词 internationalization的首末字符i和n,18为中间的字符数是“国际化”的简称. i10n为资源本地化,全称为Locali ...

  4. 用lucene.net根据关键字检索本地word文档

    目前在做一个winform小软件,其中有一个功能是能根据关键字检索本地保存的word文档.第一次是用com读取word方式(见上一篇文章),先遍历文件夹下的word文档,读取每个文档时循环关键字查找, ...

  5. C/C++软件静态测试现状

    对于C/C++软件而言,静态测试越来越趋向软件安全功能测试.包括数据机密性.完整性.可用性.不可否认性.身份认证.授权.访问控制.审计跟踪.委托.隐私保护.安全管理等. 通常情况下,C/C++静态测试 ...

  6. ASP.NET-FineUI开发

    ASP.NET-FineUI开发 随笔分类 - FineUI   ASP.NET-FineUI开发实践-10 摘要: 嵌套Grid,光棍月大放送,不藏着掖着.实在写的不好,没脸藏啊~只考虑显示排序修改 ...

  7. SD卡FAT32获得高速的文件格式(图文介绍)

    说明: MBR :Master Boot Record ( 主引导记录) DBR :DOS Boot Record ( 引导扇区) FAT :File Allocation Table ( 文件分配表 ...

  8. php-fpm介绍及配置

    php-fpm是什么 全称是php fastcgi process manager即php fastcgi进程管理器,相比fastcgi静态的唤起cgi,fpm能根据访问的压力动态的唤起cgi进程和销 ...

  9. iis处理请求随记回顾

    ----http是无状态的, 每次http请求户不影响,都是独立的:不会记的上次请求: -------iis原理:输入地址--socket封装请求体报文--发送---iis解析封装响应体---返回: ...

  10. EF codefirst+mvc4+bootstrap+autofac+ddd 系统共享 祝大家新年开心搬砖

    博客园的博友新年好,小弟在此给大伙拜了晚年,感谢一直以来的支持. 在过去的一年,从博客园有400多ASP.NET MVC爱好者加入本人的群,本人在此很感激,并勉励大家一起学习奋斗. 希望在新的一年,继 ...