您的位置:首页 > 移动开发 > Android开发

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中添加意图过滤器
<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添加意图过滤器(基础知识可知)

以后再有类似需求,就有思路了,主要是懂得看系统应用源码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息