android--创建桌面快捷方式
2016-06-23 11:27
429 查看
在安装好了应用之后,一般都会自动在桌面创建快捷方式,这篇文章就总结一下在桌面创建快捷方式的方法。
一.查看Android桌面源码Launch下Manifest
这个广播接收者接收
android:name=”com.android.launcher.action.INSTALL_SHORTCUT” 这样一个action,接收到配置上述action的广播之后,系统会根据接收广播中Intent的内容,自动为我们创建桌面快捷方式。
所以在自己的应用中生成快捷方式的步骤如下:
1. new一个Intent action配置为com.android.launcher.action.INSTALL_SHORTCUT;
2. 给new出来的intent设置图标,名称,和点击之后的意图,用于桌面上快捷方式的展示和跳转。
3. 发送广播
4. 配置权限
具体如下。
二.配置相应的Intent并发送广播
下面以本渣渣的项目为例讲解:
实现的效果为:安装了应用之后,自动在桌面生成快捷方式,点击之后跳转到应用的HomeActivity界面。
注意:
1.在点击桌面快捷方式之后,要跳转到HomeActivity界面,所以通过隐式Intent打开这个界面。
在AndroidManifest文件中给HomeActivity配置 intent-fliter
2.由源码可以看到
所以需要在AndroidManifest中添加相应的权限
3.在发送广播的时候应该是
而不是
intent:发送的广播的意图,其中配置了 图片,名称,和点击意图,也就是shortCutIntent。
shortCutIntent:点击桌面快捷方式之后所跳转的意图。
三.后记
本渣渣大三快结束了, 马上就要找工作了,正在努力准备下半年的秋招,暑假在犹豫,是呆在学校充电,还是出去找一份实习。。好纠结
一.查看Android桌面源码Launch下Manifest
<receiver android:name="com.android.launcher2.InstallShortcutReceiver" android:permission="com.android.launcher.permission.INSTALL_SHORTCUT"> <intent-filter> <action android:name="com.android.launcher.action.INSTALL_SHORTCUT" /> </intent-filter> </receiver>
这个广播接收者接收
android:name=”com.android.launcher.action.INSTALL_SHORTCUT” 这样一个action,接收到配置上述action的广播之后,系统会根据接收广播中Intent的内容,自动为我们创建桌面快捷方式。
所以在自己的应用中生成快捷方式的步骤如下:
1. new一个Intent action配置为com.android.launcher.action.INSTALL_SHORTCUT;
2. 给new出来的intent设置图标,名称,和点击之后的意图,用于桌面上快捷方式的展示和跳转。
3. 发送广播
4. 配置权限
具体如下。
二.配置相应的Intent并发送广播
下面以本渣渣的项目为例讲解:
实现的效果为:安装了应用之后,自动在桌面生成快捷方式,点击之后跳转到应用的HomeActivity界面。
//1.给intent设置图标,名称,点击后的意图 Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); //图标 intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)); //名称 intent.putExtra(Intent.EXTRA_SHORTCUT_NAME,"宇哥手机卫士"); //点击后的意图 跳转至HomeActivity 隐式意图 Intent shortCutIntent = newIntent("android.intent.action.HOME"); shortCutIntent.addCategory("android.intent.category.DEFAULT"); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,shortCutIntent ); //3.发送带数据的广播 sendBroadcast(intent);
注意:
1.在点击桌面快捷方式之后,要跳转到HomeActivity界面,所以通过隐式Intent打开这个界面。
在AndroidManifest文件中给HomeActivity配置 intent-fliter
<activity android:name="activity.HomeActivity"> <intent-filter> <action android:name="android.intent.action.HOME"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity>
2.由源码可以看到
android:permission="com.android.launcher.permission.INSTALL_SHORTCUT">
所以需要在AndroidManifest中添加相应的权限
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
3.在发送广播的时候应该是
sendBroadcast(intent);
而不是
sendBroadcast(shortCutIntent);
intent:发送的广播的意图,其中配置了 图片,名称,和点击意图,也就是shortCutIntent。
shortCutIntent:点击桌面快捷方式之后所跳转的意图。
三.后记
本渣渣大三快结束了, 马上就要找工作了,正在努力准备下半年的秋招,暑假在犹豫,是呆在学校充电,还是出去找一份实习。。好纠结
相关文章推荐
- Android Matrix详解
- Android中SeekBar和RatingBar用法实例分析
- Android6.0源码分析之蓝牙
- Android中dip、dp、sp、pt和px
- Android 优化开机启动
- Android---服务(Service)的绑定服务
- android 使用AccessibilityService 自动安装应用
- 如何查找android emulator内核的精确版本
- Monkey源码分析1—Monkey简单介绍
- Android-------数据存储
- 设置 DialogFragment 的背景颜色透明
- 解决android 应用上传三星应用市场遇到非常崩溃的问题
- Android—————— 广播接收者
- Android解析JSON数据的方法分析
- Android 标题下的内容折叠效果
- android listView嵌套gridview的使用心得
- Android 数据存储(三)之数据库存储
- Android AutoLayout全新的适配方式
- Android源码中内置包含so文件的APK文件
- Android Material Design系列之Navigation Drawer