轉載請註明出處:http://blog.csdn.net/ruils/article/details/16923201
最近研究了一下金山清理大師一鍵加速快捷方式動畫的實現(原文),順便也總結下Android中如何添加快捷方式到桌面。
方式一:
我稱之爲被動的Action方式。
在高版本的android系統(android4.3,4.4)中,從Lanucher的程序列表小部件中,長按某個小部件來添加,在低版本的android系統中,長按桌面,會出現添加快捷方式的選項。
爲了能在小部件中或者快捷方式的選項目中出現我們自己的程序,
首完要在AndroidManifest.xml中申明一個能響應"android.intent.action.CREATE_SHORTCUT"的Activity:
例如:
<activity
android:name="com.cleanmanager.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
其次,在響應這個Acitivity中設置快捷方式的屬性:
Intent shortcutIntent = new Intent();
//設置點擊快捷方式時啓動的Activity,因爲是從Lanucher中啓動,所以包名類名要寫全。
shortcutIntent.setComponent(new ComponentName(getPackageName(),
getPackageName() + "."
+ AnimationActivity.class.getSimpleName()));
//設置啓動的模式
shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
| Intent.FLAG_ACTIVITY_NEW_TASK);
Intent resultIntent = new Intent();
//設置快捷方式圖標
resultIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(this,
R.drawable.shortcut_proc_clean));
//啓動的Intent
resultIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
//設置快捷方式的名稱
resultIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
getString(R.string.app_name));
setResult(RESULT_OK, resultIntent);
finish();
最後,一定要記得setResult(RESULT_OK, resultIntent); 爲什麼稱之爲被動的Action模式呢,原因就在這裏,Lanucher找我要快捷方式,我就給它,怎麼給的呢,就是這個setResult(RESULT_OK, resultIntent);
方式二:
我稱之爲主動的發廣播方式。
首先,既然是發廣播,發給誰?當然是發給Lanucher, Lanucher要不要呢,那麼我申明一個權限,給它老闆打聲招呼,它不要也得要。老闆是誰?Android系統是也。
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
其次,就是在發廣播的Acitivity中設置快捷方式的屬性:
Intent shortcutIntent = new Intent();
//設置點擊快捷方式時啓動的Activity,因爲是從Lanucher中啓動,所以包名類名要寫全。
shortcutIntent.setComponent(new ComponentName(getPackageName(),
getPackageName() + "."
+ AnimationActivity.class.getSimpleName()));
//設置啓動的模式
shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
| Intent.FLAG_ACTIVITY_NEW_TASK);
Intent resultIntent = new Intent();
//設置快捷方式圖標
resultIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(this,
R.drawable.shortcut_proc_clean));
//啓動的Intent
resultIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
//設置快捷方式的名稱
resultIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
getString(R.string.app_name));
resultIntent.setAction(ACTION_INSTALL_SHORTCUT);
sendBroadcast(resultIntent);
ACTION_INSTALL_SHORTCUT是
public static final String ACTION_INSTALL_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";
最後就把快捷方式發給Lanucher了,這種方式是主動的,在我們的代碼邏輯中隨時可以通過廣播的方式發送快捷方式給Lanucher。
想知道這兩種方式的實現原理,請看Lanucher代碼!
第一種方式的實現原理請參考:
packages/apps/Launcher2/src/com/android/launcher2/Launcher.java 1875行
第二種方式的實現原理請參考:
packages/apps/Launcher2/src/com/android/launcher2/InstallShortcutReceiver.java
最後,如果您喜歡本文章,請點贊!
示例源碼下載:http://download.csdn.net/detail/u012379847/6604299
我把這兩種方式都寫在MainActivity中了,請注意查看
// 長按方式添加快捷方式----->即被動的Action方式。
if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
Log.d(TAG, "action " + action);
setResult(RESULT_OK, resultIntent);
finish();
} else {
// 發送廣播方式添加快捷方式----->即主動的發廣播方式
Log.d(TAG, "sendBroadcast " + resultIntent);
resultIntent.setAction(ACTION_INSTALL_SHORTCUT);
sendBroadcast(resultIntent);
}