Android如何创建桌面快捷方式
作者:bjp000111 时间:2022-09-16 18:37:06
Android创建桌面的快捷方式
概述 :创建桌面快捷方式相当与创建一个程序的入口,就像我们程序在安装完毕后会自动创建一个图标到桌面。其实创建桌面快捷方式跟创建一个程序入口差不多,但是像QQ会话一样创建一个QQ好友的会话快捷方式,就得动态的创建图标,名字了。
1.首先权限是必不可少的
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
2.然后就是在你项目配置文件里面配置
<activity
android:name="com.easemob.chatuidemo.activity.ChatActivity" >
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.CREATE_SHORTCUT" />
</intent-filter>
</activity>
这个actvity即为你要快捷方式点击后跳转的那一个activity
3.然后就是你要创建快捷方式的方法。
代码如下:
public void CreateShotCut(final Context context, final Class<?> clazz,
final String name, final String image) {
Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
// 加入action,和category之后,程序卸载的时候才会主动将该快捷方式也卸载
shortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shortcutIntent.setClass(context, clazz);
/**
* 创建一个Bundle对象让其保存将要传递的值
*/
Bundle bundle = new Bundle();
bundle.putString("userId", userId);
shortcutIntent.putExtras(bundle);
/**
* 设置这条属性,可以使点击快捷方式后关闭其他的任务栈的其他activity,然后创建指定的acticity
*/
shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// 创建快捷方式的Intent
Intent shortcut = new Intent(Intent.ACTION_CREATE_SHORTCUT);
// 不允许重复创建
shortcut.putExtra("duplicate", false);
// 点击快捷图片,运行的程序主入口
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
// 需要现实的名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
// 快捷图片
Parcelable icon = Intent.ShortcutIconResource.fromContext(
getApplicationContext(), R.drawable.ic_launcher);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
shortcut.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
context.sendBroadcast(shortcut);
}
这行代码的重要性就在如果没有这一行,那么在你点击这个快捷方式,跳转的时候就会直接跳到这个应用的栈顶(如果指定的activity在栈顶,也不会跳转其上而是销毁)而不是指定的那一个Activity(刚开始没加这条属性的时候,一直跳转不到指定的activity上)。
shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
如果想要动态的添加图片即创建快捷方式的时候获取网路上的图片来进行设置其快捷图片则使用
// Intent.EXTRA_SHORTCUT_ICON 是bitmap对象
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON,bitmap);
这行代码,你可以请求网路图片后转换为BitMap后设置进去。
ok动态的创建快捷方式就这样完成了。
标签:Android,快捷方式
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java Apache Shiro安全框架快速开发详解流程
2022-06-21 23:00:23
![](https://img.aspxhome.com/file/2023/8/69648_0s.png)
Java使用设计模式中的工厂方法模式实例解析
2021-12-07 03:57:49
![](https://img.aspxhome.com/file/2023/4/61054_0s.jpg)
Java语言实现数据结构栈代码详解
2021-12-09 16:15:05
java String的深入理解
2022-10-30 23:50:37
![](https://img.aspxhome.com/file/2023/5/69895_0s.png)
Flutter 底部弹窗如何实现多项选择
2023-06-24 17:08:17
![](https://img.aspxhome.com/file/2023/7/84707_0s.gif)
Java优先队列(PriorityQueue)重写compare操作
2022-10-02 03:59:12
SpringBoot2.0+阿里巴巴Sentinel动态限流实战(附源码)
2021-05-25 18:52:47
![](https://img.aspxhome.com/file/2023/1/83091_0s.jpg)
详解Java中while和do-while循环、break的使用
2022-10-24 13:37:04
![](https://img.aspxhome.com/file/2023/4/75684_0s.jpg)
C#设计模式之职责链模式示例详解
2023-11-08 05:26:38
Spring Cloud Gateway整合sentinel 实现流控熔断的问题
2022-01-18 23:10:05
![](https://img.aspxhome.com/file/2023/7/60407_0s.png)
Hystrix Dashboard断路监控仪表盘的实现详细介绍
2022-07-29 05:27:26
![](https://img.aspxhome.com/file/2023/3/70513_0s.png)
SpringBoot通过自定义注解实现参数校验
2023-09-21 21:11:02
![](https://img.aspxhome.com/file/2023/3/58343_0s.png)
java读取文件内容的三种方法代码片断分享(java文件操作)
2023-11-21 06:53:20
解决jmap命令打印JVM堆信息异常的问题
2023-11-05 09:07:02
![](https://img.aspxhome.com/file/2023/4/58884_0s.jpg)
MyBatis如何进行双重foreach循环
2022-11-24 22:54:17
推荐两款java开发实用工具 hutool 和 lombok
2022-04-06 10:20:14
![](https://img.aspxhome.com/file/2023/8/69938_0s.png)
SpringBoot工程搭建打包、启动jar包和war包的教程图文详解
2022-05-04 22:09:10
![](https://img.aspxhome.com/file/2023/2/69892_0s.png)
Spring探秘之如何妙用BeanPostProcessor
2021-07-01 22:41:46
java实现轻量型http代理服务器示例
2021-10-05 18:14:20
spring中使用mybatis plus连接sqlserver的方法实现
2022-11-16 12:05:09