惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
Recent Announcements
Recent Announcements
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
有赞技术团队
有赞技术团队
J
Java Code Geeks
T
The Blog of Author Tim Ferriss
MyScale Blog
MyScale Blog
人人都是产品经理
人人都是产品经理
aimingoo的专栏
aimingoo的专栏
U
Unit 42
The GitHub Blog
The GitHub Blog
云风的 BLOG
云风的 BLOG
T
Tailwind CSS Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 三生石上(FineUI控件)
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
腾讯CDC
I
InfoQ
GbyAI
GbyAI
博客园_首页

博客园 - 蔡清华

数据库中查询的各种连接(左连接,右连接,全连接,内连接,交叉连接,自连接) Android RenderScript 的使用基础篇 Android API教程:人脸检测(Face Detect) Android应用中通过AIDL机制实现进程间的通讯实例 Android开发:如何实现TCP和UDP传输 用户授权认证过程 Android应用自动更新功能的代码实现 Android蓝牙开发浅析 网络通信的中文乱码问题 (转)android中 检查网络连接状态的变化,无网络时跳转到设置界面 Android开发之OpenGL+ES教程 android 示例源码 android 图片处理 Android应用如何实现换主题功能 android 开发环境搭建 Android 之 内存管理 Android编程获取网络连接状态(3G/Wifi)及调用网络配置界面 android 网络拨号流程 Android_launcher的源码详细分析
Android应用启动后自动创建桌面快捷方式
蔡清华 · 2012-09-11 · via 博客园 - 蔡清华

Posted on 2012-09-11 10:54  蔡清华  阅读(339)  评论()    收藏  举报

和IOS开发和Windows Phone开发相比,Android是开放的,Android上的开发也相对更加灵活,能够做很多事情。有的朋友会发现,在某些Android应用安装以后,第一次运行,就会在桌面创建快捷方式。这是如何做到的呢?

要不怎么说Android特别开放呢,在Android开发中,只要发送一个广播,就可以实现这种需求了。

废话不多说,以下是封装好的一段代码。

复制代码

 1 public class ShortcutUtil {  
 2   
 3     public static void createShortCut(Activity act, int iconResId,  
 4             int appnameResId) {  
 5   
 6         // com.android.launcher.permission.INSTALL_SHORTCUT  
 7   
 8         Intent shortcutintent = new Intent(  
 9                 "com.android.launcher.action.INSTALL_SHORTCUT");  
10         // 不允许重复创建  
11         shortcutintent.putExtra("duplicate", false);  
12         // 需要现实的名称  
13         shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME,  
14                 act.getString(appnameResId));  
15         // 快捷图片  
16         Parcelable icon = Intent.ShortcutIconResource.fromContext(  
17                 act.getApplicationContext(), iconResId);  
18         shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);  
19         // 点击快捷图片,运行的程序主入口  
20         shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,  
21                 new Intent(act.getApplicationContext(), act.getClass()));  
22         // 发送广播  
23         act.sendBroadcast(shortcutintent);  
24     }  
25 }  

复制代码

代码比较简单,不做更详细的解释。

别忘记增加以下权限,否则看不到任何效果。

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>

另外,这样做可能并不友好。更好的做法是,第一次运行程序的时候,提示用户是否创建桌面快捷方式,让用户选择。以后再次运行就不再进行提示了。