<一>開機自啟動
當Android啟動時,會發出一個系統廣播,內容為ACTION_BOOT_COMPLETED,它的字符串常量表示為android.intent.action.BOOT_COMPLETED。只要在程序中“捕捉”到這個消息,再啟動之即可。我們要做的是做好接收這個消息的准備,而實現的手段就是實現一個BroadcastReceiver。
1.首先定義一個BroadcastReceiver,覆寫其onReceive()方法,在裡面判斷intent是否是開機啟動廣播,如果是的話就進行相應的處理;
public class BootBroadcastReceiver extends BroadcastReceiver {
static final String BOOT_ACTION = "android.intent.action.BOOT_COMPLETED";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(BOOT_ACTION)) {
// doSomething(startService or startAcvitity or downLoadFile ...)
}
}
}
2.在Manifest文件中進行配置,intent-filter表示該Receiver接收的廣播消息為:android.intent.action.BOOT_COMPLETED;
<receiver android:name="com.xxx.BootBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <!-- uses 權限 -->
<二>添加刪除桌面快捷方式
有時候希望自動將程序快捷方式添加到桌面,最近在一個項目中,就遇到這樣的需求,現將自己在做法進行總結及延伸。
1.添加:查看Launcher源碼,查看是如何添加桌面快捷方式的,發現Launcher通過自己注冊的InstallShortCutReceiver和UnInstallShortCutReceiver實現快捷方式圖標的生成與移除過程;
<receiver
android:name="com.android.launcher2.InstallShortcutReceiver"<!--SDK版本小於8時為launcher-->
android:permission="com.android.launcher.permission.INSTALL_SHORTCUT" >
<intent-filter>
<action android:name="com.android.launcher.action.INSTALL_SHORTCUT" />
</intent-filter>
</receiver>
於是乎就可以發送一個廣播給Launcher,Launcher接收到此廣播之後就可以將快捷方式添加到桌面,並且需要添加權限
public void addShortcut() {
// 創建快捷方式的Intent
Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
// 不允許重復創建
shortcutIntent.putExtra("duplicate", false);
// 快捷方式的名稱
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
getString(R.string.app_name));
// 快捷圖片,一個Parcelable對象
Parcelable icon = Intent.ShortcutIconResource.fromContext(
getApplicationContext(), R.drawable.ic_launcher);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
// 點擊快捷圖片,運行的程序主入口
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
sendBroadcast(shortcutIntent);
}
添加權限:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
2.刪除:刪除快捷方式用得不多,上面的方式添加到桌面的快捷方式,在程序卸載的時候也會自動從桌面刪除;
public static void delShortcut(Context context) {
Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
// 獲取當前應用名稱的另一種方式
String title = null;
try {
final PackageManager pm = context.getPackageManager();
title = pm.getApplicationLabel(
pm.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA)).toString();
} catch (Exception e) {
}
// 快捷方式名稱
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
Intent intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
context.sendBroadcast(shortcut);
}
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
3.判斷桌面快捷方式是否已經存在
public static boolean hasShortcut(Context cx) {
boolean result = false;
// 獲取當前應用名稱
String title = null;
try {
final PackageManager pm = cx.getPackageManager();
title = pm.getApplicationLabel(
pm.getApplicationInfo(cx.getPackageName(), PackageManager.GET_META_DATA)).toString();
} catch (Exception e) {
}
final String uriStr;
if (android.os.Build.VERSION.SDK_INT < 8) {
uriStr = "content://com.android.launcher.settings/favorites?notify=true";
} else {
uriStr = "content://com.android.launcher2.settings/favorites?notify=true";
}
final Uri CONTENT_URI = Uri.parse(uriStr);
final Cursor c = cx.getContentResolver().query(CONTENT_URI, null, "title=?", new String[] { title }, null);
if (c != null && c.getCount() > 0) {
result = true;
}
return result;
}
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
幾個相關的Action
// 系統啟動完成
static final String BOOT_COMPLETED_ACTION = "android.intent.action.BOOT_COMPLETED";
// 設備上新安裝了一個應用程序包
static final String PACKAGE_ADDED_ACTION = "android.intent.action.PACKAGE_ADDED";
// 設備上刪除了一個應用程序包
static final String PACKAGE_REMOVED_ACTION = "android.intent.action.PACKAGE_REMOVED";
// 刪除應用程序快捷方式,需要如下權限
// com.android.launcher.permission.UNINSTALL_SHORTCUT
static final String UNINSTALL_SHORTCUT_ACTION = "com.android.launcher.action.UNINSTALL_SHORTCUT";
// 添加快捷方式,需要如下權限
// com.android.launcher.permission.INSTALL_SHORTCUT
static final String INSTALL_SHORTCUT_ACTION = "com.android.launcher.permission.INSTALL_SHORTCUT";
4.監聽app安裝/卸載過程,需要用到上面的PACKAGE_ADDED和PACKAGE_REMOVED兩個Action,可以對獲取到的應用程序包名進行相應的判斷處理;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(PACKAGE_ADDED_ACTION)) {
// doSomething ...獲取應用程序包名
String packageName = intent.getDataString();
}
}
添加如下配置,對Receiver進行配置
<receiver android:name="com.example.async.BootBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" /> <!--PACKAGE_REMOVED-->
<data android:scheme="package" /> <!-- 一定要添加此節點 -->
</intent-filter>
</receiver>
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11