Launcher就是我们Android系统的手机桌面,这里一直是纷繁手机应用世界的必争之地,几乎每个应用都希望在这里有一席之地。
一个Launcher一般由以下几个部分组成:
今天我们先来做一个最简单的手机应用快捷方式添加。手机应用桌面快捷方式的基本原理是:
- 采用了 Android 系统的广播机制,发送一个广播“com.android.launcher.action.INSTALL_SHORTCUT”,Android 系统的 Launcher 中的 InstallShortcutReceiver 在接收到这个广播之后,快捷图标就会被创建。
手机应用快捷方式的添加总共分成三大步:
- 创建一个添加快捷方式的Intent,该Intent的action属性值是“com.android.launcher.action.INSTALL_SHORTCUT”;
- 通过该Intent添加Extra属性来设置快捷方式的标题、图标以及快捷方式要启动的应用程序;
- 调用sendBroadcast()方法发送广播即可添加快捷方式
另外,创建快捷方式是需要相应权限的,不要忘了
- <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
- <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>
- package farsight.com.cn.addicon;
- import android.content.Intent;
- import android.database.Cursor;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Parcelable;
- import android.support.v7.app.ActionBarActivity;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Button;
- public class MainActivity extends ActionBarActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Button button = (Button) findViewById(R.id.button);
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if(!isExist()) {
- // 创建添加快捷方式的Intent
- Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
- // 加载快捷方式的应用程序名称
- String tile = getResources().getString(R.string.app_name);
- // 加载快捷方式的图标
- Parcelable icon = Intent.ShortcutIconResource.fromContext(MainActivity.this, R.drawable.ic_launcher);
- // 创建点击快捷方式后操作Intent,该处当点击创建的快捷方式后,再次启动该程序
- Intent myIntent = new Intent(MainActivity.this,
- MainActivity.class);
- // 设置快捷方式的标题
- intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, tile);
- // 设置快捷方式的图标
- intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
- // 设置快捷方式对应的Intent
- intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent);
- // 发送广播添加快捷方式
- sendBroadcast(intent);
- }
- }
- });
- }
- private boolean isExist() {
- boolean isExist = false;
- int version = getSdkVersion();
- Uri uri = null;
- //桌面快捷图标的数据保存在手机的 /data/data/com.android.launcher/databases/launcher.db 中,
- // 同时系统对外提供了一个 LauncherProvider 供外界进行访问。
- // 在 launcher.db 的 favorites 表中保存了具体的数据,
- // 可以通过 LauncherProvider 中的 authorities 加 favorites 的形式来访问 favorites 表;
- // 考虑到程序的兼容性 authorities 的值要根据不同的 SDK 版本来进行设置,
- // 在 SDK 版本 2.0 之前 authorities 的值为:com.android.launcher.settings,
- // 而在 SDK 2.0 之后 authorities 的值为:com.android.launcher2.settings;
- if (version < 2.0) {
- uri = Uri.parse("content://com.android.launcher.settings/favorites");
- } else {
- uri = Uri.parse("content://com.android.launcher2.settings/favorites");
- }
- String selection = " title = ?";
- String[] selectionArgs = new String[] { getResources().getString(R.string.app_name).toString() };
- Cursor cursor = getContentResolver().query(uri, null, selection, selectionArgs, null);
- if (cursor != null && cursor.getCount() > 0) {
- isExist = true;
- }
- if (cursor != null) {
- cursor.close();
- }
- return isExist;
- }
- /**
- * 得到当前系统SDK版本
- */
- private int getSdkVersion() {
- return android.os.Build.VERSION.SDK_INT;
- }
- }
AndroidManifest.xml
- <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="farsight.com.cn.addicon">
- <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
- <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity
- android:name=".MainActivity"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest></span><span style="font-size:12px;">
- </span>