android点滴(24)之android 创建、删除、判断是否存在快捷方式
创建和删除快捷方式主要是Launcher完成的,我们只是把快捷方式的名称、图标、关联的activity信息传递给Launcher而已。
我们构造好了Intent信息后,将其以广播的方式发送出去,Launcher会帮忙完成创建、删除,并且会弹出一个Toast。
1.下面是系统中Launcher的Manifest文件。从中可以看到它定义了创建和删除快捷方式的permission,因此我们必须在自己的应用中声明权限才行。
com.android.launcher.permission.INSTALL_SHORTCUT 创建权限
com.android.launcher.permission.UNINSTALL_SHORTCUT 删除权限
2.创建和删除快捷方式的广播为InstallShortcutReceiver 和 UninstallShortcutReceiver。
3.Launcher为我们提供了ContentProvider LauncherProvider,我们可以通过查看它来获取已经存在的快捷方式的信息 。
这个也是要有permission(com.android.launcher.permission.READ_SETTINGS) 。
Launcher的manifest文件
xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.launcher">
<original-package android:name="com.android.launcher2" />
<permission
android:name="com.android.launcher.permission.INSTALL_SHORTCUT"
android:permissionGroup="android.permission-group.SYSTEM_TOOLS"
android:protectionLevel="normal"
android:label="@string/permlab_install_shortcut"
android:description="@string/permdesc_install_shortcut" />
<permission
android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"
android:permissionGroup="android.permission-group.SYSTEM_TOOLS"
android:protectionLevel="normal"
android:label="@string/permlab_uninstall_shortcut"
android:description="@string/permdesc_uninstall_shortcut"/>
<permission
android:name="com.android.launcher.permission.READ_SETTINGS"
android:permissionGroup="android.permission-group.SYSTEM_TOOLS"
android:protectionLevel="normal"
android:label="@string/permlab_read_settings"
android:description="@string/permdesc_read_settings"/>
<permission
android:name="com.android.launcher.permission.WRITE_SETTINGS"
android:permissionGroup="android.permission-group.SYSTEM_TOOLS"
android:protectionLevel="normal"
android:label="@string/permlab_write_settings"
android:description="@string/permdesc_write_settings"/>
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.SET_WALLPAPER_HINTS" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.BIND_APPWIDGET" />
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
<uses-permission android:name="com.android.launcher.permission.WRITE_SETTINGS" />
<application
android:name="com.android.launcher2.LauncherApplication"
android:label="@string/application_name"
android:icon="@drawable/ic_launcher_home"
android:hardwareAccelerated="@bool/config_hardwareAccelerated"
android:largeHeap="@bool/config_largeHeap">
<activity
android:name="com.android.launcher2.Launcher"
android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
android:stateNotNeeded="true"
android:theme="@style/Theme"
android:windowSoftInputMode="adjustPan"
android:screenOrientation="nosensor">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.MONKEY"/>
</intent-filter>
</activity>
<activity
android:name="com.android.launcher2.WallpaperChooser"
style="@style/Theme.WallpaperPicker"
android:label="@string/pick_wallpaper"
android:icon="@drawable/ic_launcher_wallpaper"
android:finishOnCloseSystemDialogs="true"
android:process=":wallpaper_chooser">
<intent-filter>
<action android:name="android.intent.action.SET_WALLPAPER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android:name="android.wallpaper.preview"
android:resource="@xml/wallpaper_picker_preview" />
</activity>
<activity android:name="com.android.launcher2.RocketLauncher"
android:label="@string/dream_name"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.DREAM" />
</intent-filter>
</activity>
<!-- 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>
<!-- Intent received used to uninstall shortcuts from other applications -->
<receiver
android:name="com.android.launcher2.UninstallShortcutReceiver"
android:permission="com.android.launcher.permission.UNINSTALL_SHORTCUT">
<intent-filter>
<action android:name="com.android.launcher.action.UNINSTALL_SHORTCUT" />
</intent-filter>
</receiver>
<!-- The settings provider contains Home's data, like the workspace favorites -->
<provider
android:name="com.android.launcher2.LauncherProvider"
android:authorities="com.android.launcher2.settings"
android:writePermission="com.android.launcher.permission.WRITE_SETTINGS"
android:readPermission="com.android.launcher.permission.READ_SETTINGS" />
</application>
</manifest>
4.Launcher的表创建如下
(
_id INTEGER PRIMARY KEY,
title TEXT,
intent TEXT,
container INTEGER,
screen INTEGER,
cellX INTEGER,
cellY INTEGER,
spanX INTEGER,
spanY INTEGER,
itemType INTEGER,
appWidgetId INTEGER NOT NULL DEFAULT -1,
isShortcut INTEGER,
iconType INTEGER,
iconPackage TEXT,
iconResource TEXT,
icon BLOB,
uri TEXT,
displayMode INTEGER
);
下面是一个实例程序。
源码:
2
3 import android.app.Activity;
4 import android.content.ContentResolver;
5 import android.content.Intent;
6 import android.database.Cursor;
7 import android.net.Uri;
8 import android.os.Build;
9 import android.os.Bundle;
10 import android.os.Parcelable;
11 import android.view.View;
12 import android.widget.Button;
13 import android.widget.Toast;
14
15 public class ShortcutsActivity extends Activity {
16 private static final String EXTRA_DATA = "extra_data";
17 @Override
18 public void onCreate(Bundle savedInstanceState) {
19 super.onCreate(savedInstanceState);
20 setContentView(R.layout.main);
21 Button createBtn = (Button) findViewById(R.id.btn_create);
22 createBtn.setOnClickListener(new View.OnClickListener() {
23
24 @Override
25 public void onClick(View v) {
26 createShortcut();
27 }
28 });
29 Button deleteBtn = (Button) findViewById(R.id.btn_delete);
30 deleteBtn.setOnClickListener(new View.OnClickListener() {
31
32 @Override
33 public void onClick(View v) {
34 deleteShortcut();
35 }
36
37 });
38 }
39 /**
40 * 删除快捷方式
41 */
42 private void deleteShortcut() {
43 Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
44 shortcutIntent.setClassName(this, this.getClass().getName());
45
46 Intent intent = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
47 intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
48 intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
49 sendBroadcast(intent);
50 }
51 /**
52 * 创建快捷方式
53 */
54 private void createShortcut() {
55 if(hasShortcut()){
56 Toast.makeText(this, "快捷方式已經存在", Toast.LENGTH_SHORT).show();
57 return;
58 }
59 //启动可以Activity的Intent
60 Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
61 shortcutIntent.setClassName(this, this.getClass().getName());
62 shortcutIntent.putExtra(EXTRA_DATA, "SHORTCUT");
63
64 //创建快捷方式的Intent
65 Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
66 intent.putExtra("duplicate", false);
67 intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
68 intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
69 Parcelable iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher);
70 intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
71 //发送广播
72 sendBroadcast(intent);
73 }
74 /**
75 * 根据 title 判断快捷方式是否存在
76 * @return
77 */
78 private boolean hasShortcut() {
79 String url;
80 if(getSystemVersion() < 8){
81 url = "content://com.android.launcher.settings/favorites?notify=true";
82 }else{
83 url = "content://com.android.launcher2.settings/favorites?notify=true";
84 }
85
86 ContentResolver resolver = getContentResolver();
87 Cursor cursor = resolver.query(Uri.parse(url), new String[]{"title","iconResource"},
88 "title=?", new String[]{getString(R.string.app_name)}, null);
89 if(cursor != null && cursor.getCount() > 0){
90 return true;
91 }
92 return false;
93 }
94 /**
95 * 获取系统的SDK版本号
96 * @return
97 */
98 private int getSystemVersion(){
99 return Build.VERSION.SDK_INT;
100 }
101 }
manifest文件
2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="com.test.shortcuts"
4 android:versionCode="1"
5 android:versionName="1.0" >
6
7 <uses-sdk android:minSdkVersion="10" />
8 <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
9 <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>
10 <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>
11
12 <application
13 android:icon="@drawable/ic_launcher"
14 android:label="@string/app_name" >
15 <activity
16 android:label="@string/app_name"
17 android:name=".ShortcutsActivity" >
18 <intent-filter >
19 <action android:name="android.intent.action.MAIN" />
20
21 <category android:name="android.intent.category.LAUNCHER" />
22 </intent-filter>
23 </activity>
24 </application>
25
26 </manifest>