Android — 创建快捷方式
来源:互联网 发布:谁人知歌词 编辑:程序博客网 时间:2024/05/16 15:29
创建桌面快捷方式
说明
通常快捷方式都至少包含如下三个部分:
- 名称: 快捷方式要显示的名字
- 图标: 快捷方式要显示的图标
- 动作: 点击快捷方式要做的事情
Android 中创建快捷方式有两种方式:
长按应用拖拽到桌面, 这种方式创建的快捷方式就是应用本身的快照,效果和点击应用本身是一样的,这里就不详细描述了。
使用代码的方式创建快捷方式,这种方式是可以自定义快捷方式的,本文就举例简单说明这种方式。
创建快捷方式的方法比较简单,其实就是:发送广播告知系统要创建快捷方式。
发送广播就需要使用 Intent ,这里的 Intent 就要传递上面提到的三部分数据。
创建快捷方式是需要权限的:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
Demo
主要代码部分:
/** * 创建桌面快捷方式 * * 创建快捷方式(发送广播告知系统要创建快捷方式) * Intent需要包含: * 1.名称 * 2.图标 * 3.动作 * * 权限:com.android.launcher.permission.INSTALL_SHORTCUT * * action: com.android.launcher.action.INSTALL_SHORTCUT */public void installShortcut() {//创建广播要使用的 Intent Intent intent = new Intent(); //需要设置 action intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); //1.快捷方式的名称 intent.putExtra(Intent.EXTRA_SHORTCUT_NAME,"Demo"); //2.快捷方式的图标 intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, BitmapFactory .decodeResource(getResources(), R.mipmap.ic_launcher)); //3.快捷方式的动作(点击响应),根据不同的应用需求设置//这里是打开某 activity 的动作,隐式启动,因此需要在 Manifest 文件中配置 Intent action = new Intent("com.yu.demos.action.SHORTCUT"); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,action); //不允许重复创建 intent.putExtra("duplicate", false); //发送广播 sendBroadcast(intent);}
本例要配置 AndroidManifest.xml ,为快捷方式要启动的 activity 添加如下:
<intent-filter> <action android:name="com.yu.demos.action.SHORTCUT"/> <category android:name="android.intent.category.DEFAULT"/></intent-filter>
示例:
<activity android:name=".activity.MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> <action android:name="com.yu.demos.action.SHORTCUT"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter></activity>
当然你应该根据自己的需求设置相应的动作,如访问网页,拨打电话,打开其他应用等…
效果图
0 0
- Android — 创建快捷方式
- Android 创建快捷方式 删除快捷方式
- Android之快捷方式一——通过应用程序创建快捷方式
- Android创建快捷方式,Phonegap创建快捷方式
- Android创建快捷方式(shortcut)
- android 代码创建快捷方式
- android创建快捷方式
- android 创建快捷方式
- Android:创建快捷方式
- android 创建桌面快捷方式
- android 创建快捷方式
- android创建任意快捷方式
- Android创建快捷方式
- android 创建快捷方式
- Android--创建快捷方式
- Android 创建桌面快捷方式
- android 创建快捷方式
- Android之创建快捷方式
- 深入理解G1垃圾收集器
- Android一面而就最全面试题汇总
- cocos creator第一个星星游戏
- DataTable和list转换
- python 中字典打印中文问题
- Android — 创建快捷方式
- 【设计模式】单例模式的常见应用场景
- android如何保证service不被杀死
- Solr 嵌套文档
- apache+tomcat整合的好处与原理
- URL的常用方法
- 重写toString方法-日志
- 使用余弦定理计算两篇文章的相似性
- 关于eclipse的maven项目遇到的问题