总结:android 创建快捷方式的两种方式+判断是否已经创建+删除快捷方式
在AndroidManifest.xml 文件中声明 创建和删除快捷方式时声明权限
发布日期:2021-07-01 05:57:05
浏览次数:3
分类:技术文章
本文共 4148 字,大约阅读时间需要 13 分钟。
1. 在清单文件里面进行注册:例如:
这样,就会自动加入到 系统launcher的快捷方式里面
2. 手动创建快捷方式
public static void createSystemSwitcherShortCut(Context context) { final Intent addIntent = new Intent( "com.android.launcher.action.INSTALL_SHORTCUT"); final Parcelable icon = Intent.ShortcutIconResource.fromContext( context, R.drawable.ic_switcher_shortcut); // 获取快捷键的图标 addIntent.putExtra("duplicate", false); final Intent myIntent = new Intent(context, SystemSwitcherActivity.class); myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, context.getString(R.string.switch_widget));// 快捷方式的标题 addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);// 快捷方式的图标 addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent);// 快捷方式的动作 context.sendBroadcast(addIntent); }更具有通用性的写法如下:
/** * 为程序创建桌面快捷方式 */ private void addShortcut(){ Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); //快捷方式的名称 shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name)); shortcut.putExtra("duplicate", false); //不允许重复创建 /****************************此方法已失效*************************/ //ComponentName comp = new ComponentName(this.getPackageName(), "."+this.getLocalClassName()); //shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp)); /******************************end*******************************/ Intent shortcutIntent = new Intent(Intent.ACTION_MAIN); shortcutIntent.setClassName(this, this.getClass().getName()); shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); //快捷方式的图标 ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon); shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes); sendBroadcast(shortcut); }
小结:一般做法是在设置里面加上手动创建快捷方式的设置。
在程序第一次启动的时候,手动创建一次快捷方式。
3.判断是否已经创建了快捷方式(在某些机型中需要判断)
private boolean hasShortcut(){ boolean isInstallShortcut = false; final ContentResolver cr = activity.getContentResolver(); final String AUTHORITY ="com.android.launcher.settings"; final Uri CONTENT_URI = Uri.parse("content://" +AUTHORITY + "/favorites?notify=true"); Cursor c = cr.query(CONTENT_URI,new String[] {"title","iconResource" },"title=?", new String[] {mapViewActivity.getString(R.string.app_name).trim()}, null); if(c!=null && c.getCount()>0){ isInstallShortcut = true ; } return isInstallShortcut ;
4.删除
/** * 删除程序的快捷方式 */ private void delShortcut(){ Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT"); //快捷方式的名称 shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name)); String appClass = this.getPackageName() + "." +this.getLocalClassName(); ComponentName comp = new ComponentName(this.getPackageName(), appClass); shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp)); sendBroadcast(shortcut); }
5.声明权限
6.一个应用开启多个的问题
现在都很流行在桌面上为自己的应用创建快捷方式,网上也很多例子,但是基本都是同一个,创建快捷方式的手法是对的,但是通过快捷方式开启自己的应用的时候会发现程序菜单里头打开的应用和桌面快捷方式打开的应用竟然不是同一个,这样会导致一个应用开启了多个。 一开始从activity的加载模式入手,把默认的activity的加载模式设置成了android:launchMode="singleInstance" 虽然每次都只开启了同一个页面,但是每次都是从新加载的,而不是直接调到已经开启的activity,这个交互很不理想; 经过仔细研究log发现,通过桌面快捷方式开启应用和通过程序菜单开启应用的日志信息差了一个东西cat=[android.intent.category.LAUNCHER],通过快捷方式开启应用并没有打印出这个属性,所以就尝试在快捷方式创建的时候给他的intent添加一个属性,
ComponentName comp = new ComponentName(this.getPackageName(), this.getPackageName() + "." +this.getLocalClassName()); Intent intent = new Intent(Intent.ACTION_MAIN).setComponent(comp); intent.addCategory(Intent.CATEGORY_LAUNCHER); shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);其中,关键的就是intent.addCategory(Intent.CATEGORY_LAUNCHER); 完美解决了标题提到的问题!
转载地址:https://panda1234lee.blog.csdn.net/article/details/9042873 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月19日 23时30分30秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Java判断字符串是否为数字(浮点类型也包括)
2019-05-01
ubuntu opencv-python 安装很慢问题
2019-05-01
MySQL5.7版本修改了my.ini配置文件后mysql服务无法启动问题
2019-05-01
Azkaban体系结构
2019-05-01
机器学习之重头戏-特征预处理
2019-05-01
synchronized底层实现及锁的升级、降级
2019-05-01
PermGen space-永久区内存溢出
2019-05-01
Maven继承和聚合
2019-05-01
Apache Kafka:优化部署的 10 种最佳实践
2019-05-01
Leetcode 35. 搜索插入位置 c#
2019-05-01
[9] JMeter-常用函数的使用
2019-05-01
[12] JMeter-结果分析之图形图表
2019-05-01
使用aspose.words 18.6实现pdf文档转换
2019-05-01
Java数组详解
2019-05-01
vs中动态DLL与静态LIB工程中加入版本信息的方法
2019-05-01
大数据分析技术与应用一站式学习(值得收藏)_v20200418
2019-05-01
自定义Starter
2019-05-01
映 射 ALT 键
2019-05-01