android创建快捷方式
2016-06-20 21:55
525 查看
前言:不要去记代码,要理解原理,代码千千万,永远记不完,理解了原理,才可以写出自己的代码
我们先讲原理,再讲实现:
在android系统上为一个应用创建快捷方式,当我们第一次有这种需求的时候,感觉一筹莫展,但仔细想想,还是有迹可循,且听我慢慢道来:
快捷方式是在桌面上的,桌面是一个独立的应用,我们的需求可以转换为:我们想要通知一个应用(桌面),做某件事情(创建快捷方式),那么
我们自然而然的想到了广播,那么桌面应用中有没有这样一个广播呢?查看桌面源码中的清单文件我们不仅会心一笑
<!-- Intent received used to install shortcuts from other applications -->
<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>注释已经很清楚了,我们只需要按照上面的要求发送一个广播通知桌面创建一个快捷方式就可以了:
1.上面广播中有一个自定义权限,需要在我们自己的应用的清单文件加入这个权限
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
2.在我们的代码中构造一个intent发送一个广播给桌面
这里又有麻烦了,我们应该构造一个什么样的intent呢?从我们的需求入手,我们想要创建一个快捷方式,那么肯定有快捷方式的名称,
图标,以及快捷方式的动作,我们的动作是想点击这个快捷方式的时候,跳转到我们应用的Activity,一想到跳转activity,我们知道是用intent来跳转的,
看来我们需要两个intent,一个intent发送广播用,一个intent启动我们的activity用,那么我们具体该怎么构造呢?那就要看桌面应用的源码了,看它
如何处理创建快捷方式这个广播的,我们只要根据它的处理来构造就可以了:
private boolean installShortcut(Context context, Intent data, int screen) {
String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
if (findEmptyCell(context, mCoordinates, screen)) {
CellLayout.CellInfo cell = new CellLayout.CellInfo();
cell.cellX = mCoordinates[0];
cell.cellY = mCoordinates[1];
cell.screen = screen;
Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
if (intent.getAction() == null) {
intent.setAction(Intent.ACTION_VIEW);
}
// By default, we allow for duplicate entries (located in
// different places)
boolean duplicate = data.getBooleanExtra(Launcher.EXTRA_SHORTCUT_DUPLICATE, true);
if (duplicate || !LauncherModel.shortcutExists(context, name, intent)) {
((LauncherApplication)context.getApplicationContext()).getModel()
.addShortcut(context, data, cell, true);
Toast.makeText(context, context.getString(R.string.shortcut_installed, name),
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, context.getString(R.string.shortcut_duplicate, name),
Toast.LENGTH_SHORT).show();
}
return true;
} else {
Toast.makeText(context, context.getString(R.string.out_of_space),
Toast.LENGTH_SHORT).show();
}
return false;
}
根据桌面应用的源码我们可以知道,我们的intent应该这样构造:
//构造intent,这个intent要发送给桌面
Intent intent = new Intent();
//我们要创建快捷方式
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
//禁止重复创建
intent.putExtra("duplicate",false);
//这个initent就是我们快捷方式的动作,就是启动我们的activity
Intent doWhatIntent = new Intent();
//桌面不知道我们的activity,所以我们只能通过在我们的activity中添加一个意图过滤器来识别这个广播
doWhatIntent.setAction("aaa.bbb.ccc");
//快捷方式图标
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
//快捷方式名称
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME,"我的快捷方式");
//快捷方式动作
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,doWhatIntent);
//发送广播
sendBroadcast(intent);别忘了在我们的activity中添加意图过滤器
总结:
创建快捷方式步骤:
1.添加权限(桌面应用清单文件中获得)
2.构造intent(桌面应用源码中可知)
3.为要启动的activity添加意图过滤器(基础知识可知)
以后再有类似需求,就有思路了,主要是懂得看系统应用源码
我们先讲原理,再讲实现:
在android系统上为一个应用创建快捷方式,当我们第一次有这种需求的时候,感觉一筹莫展,但仔细想想,还是有迹可循,且听我慢慢道来:
快捷方式是在桌面上的,桌面是一个独立的应用,我们的需求可以转换为:我们想要通知一个应用(桌面),做某件事情(创建快捷方式),那么
我们自然而然的想到了广播,那么桌面应用中有没有这样一个广播呢?查看桌面源码中的清单文件我们不仅会心一笑
<!-- Intent received used to install shortcuts from other applications -->
<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>注释已经很清楚了,我们只需要按照上面的要求发送一个广播通知桌面创建一个快捷方式就可以了:
1.上面广播中有一个自定义权限,需要在我们自己的应用的清单文件加入这个权限
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
2.在我们的代码中构造一个intent发送一个广播给桌面
这里又有麻烦了,我们应该构造一个什么样的intent呢?从我们的需求入手,我们想要创建一个快捷方式,那么肯定有快捷方式的名称,
图标,以及快捷方式的动作,我们的动作是想点击这个快捷方式的时候,跳转到我们应用的Activity,一想到跳转activity,我们知道是用intent来跳转的,
看来我们需要两个intent,一个intent发送广播用,一个intent启动我们的activity用,那么我们具体该怎么构造呢?那就要看桌面应用的源码了,看它
如何处理创建快捷方式这个广播的,我们只要根据它的处理来构造就可以了:
private boolean installShortcut(Context context, Intent data, int screen) {
String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
if (findEmptyCell(context, mCoordinates, screen)) {
CellLayout.CellInfo cell = new CellLayout.CellInfo();
cell.cellX = mCoordinates[0];
cell.cellY = mCoordinates[1];
cell.screen = screen;
Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
if (intent.getAction() == null) {
intent.setAction(Intent.ACTION_VIEW);
}
// By default, we allow for duplicate entries (located in
// different places)
boolean duplicate = data.getBooleanExtra(Launcher.EXTRA_SHORTCUT_DUPLICATE, true);
if (duplicate || !LauncherModel.shortcutExists(context, name, intent)) {
((LauncherApplication)context.getApplicationContext()).getModel()
.addShortcut(context, data, cell, true);
Toast.makeText(context, context.getString(R.string.shortcut_installed, name),
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, context.getString(R.string.shortcut_duplicate, name),
Toast.LENGTH_SHORT).show();
}
return true;
} else {
Toast.makeText(context, context.getString(R.string.out_of_space),
Toast.LENGTH_SHORT).show();
}
return false;
}
根据桌面应用的源码我们可以知道,我们的intent应该这样构造:
//构造intent,这个intent要发送给桌面
Intent intent = new Intent();
//我们要创建快捷方式
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
//禁止重复创建
intent.putExtra("duplicate",false);
//这个initent就是我们快捷方式的动作,就是启动我们的activity
Intent doWhatIntent = new Intent();
//桌面不知道我们的activity,所以我们只能通过在我们的activity中添加一个意图过滤器来识别这个广播
doWhatIntent.setAction("aaa.bbb.ccc");
//快捷方式图标
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
//快捷方式名称
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME,"我的快捷方式");
//快捷方式动作
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,doWhatIntent);
//发送广播
sendBroadcast(intent);别忘了在我们的activity中添加意图过滤器
<activity android:name=".activity.HomeActivity" > <intent-filter> <action android:name="aaa.bbb.ccc" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>这样当点击我们创建的快捷方式的时候,我们就能接受到广播,启动activity了。
总结:
创建快捷方式步骤:
1.添加权限(桌面应用清单文件中获得)
2.构造intent(桌面应用源码中可知)
3.为要启动的activity添加意图过滤器(基础知识可知)
以后再有类似需求,就有思路了,主要是懂得看系统应用源码
相关文章推荐
- Cannot resolve symbol'R'产生的原因和解决方法
- Android 平滑图片加载和缓存库 Glide
- android 应用按返回键仍然在后台运行
- android5.0用UsageStatsManager来获取程序运行时间
- Android中的Activity4中启动模式使用场景
- Android Studio教程--Android Studio 2.1安装与配置
- 安卓中计算 TextView 的宽高、打点
- git的创建新项目的使用
- Android-Android studio 出现 Error: NDK integration is deprecated in the current plugin. 问题解决
- android 连接 wcf rest注意点
- Android(Java)之多线程结果返回——Future 、FutureTask、Callable、Runnable
- LruCache详解之 Android 内存优化
- "xxx" is not translated in "en" (English)
- android中自定义dialog的使用方法简介
- android 开发Handler源码剖析
- android 开发Handler源码剖析
- android 开发Handler源码剖析
- Android ViewDragHelper完全解析 自定义ViewGroup神器
- 菜鸟学android之二--OnClickListener的三种实现方法 ,acticity之间的传值,数值格式化(附实例:计算BMI数值)
- 基于Android Studio的内存泄漏检测与解决全攻略