Androidjava
前一個問題是論壇裏的一位朋友提出來的:「如何在應用安裝到手機裏時,自動在桌面增長快捷方式?」 ,第二個問題是在網上看到的:「apk安裝後如何自啓動」 。app
很顯然,除非在應用安裝後有相關的廣播能被捕獲到,不然就無法作了,事實是有的:Intent.ACTION_PACKAGE_ADDED。ide
Launcher中的應用列表正是這麼作的:this
<Launcher.java>
spa
/** xml
* Registers various intent receivers. The current implementation registers blog
* only a wallpaper intent receiver to let other applications change the 事件
* wallpaper.
*/
private void registerIntentReceivers() {
IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
filter.addAction(Intent.ACTION_PACKjAGE_REMOVED);
filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
filter.addDataScheme("package");
registerReceiver(mApplicationsReceiver, filter);
filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
registerReceiver(mCloseSystemDialogsReceiver, filter);
}
我依樣畫葫蘆嘗試了一下,直接在個人應用中實現BroadcastReceiver並在XML註冊,對action=Intent.ACTION_PACKAGE_ADDED進行捕獲,可是沒捕獲到。
後來一查文檔才發現這是行不通的:
public static final String ACTION_PACKAGE_ADDED
Broadcast Action: A new application package has been installed on the device. The data contains the name of the package. Note that the newly installed package does not receive this broadcast.
看來在應用自身中經過BroadcastReceiver來捕獲Add消息是不行的,可是我想到了另外一種折中的實現方法——經過另外一個應用來輔助實現。
須要先實現一個包含對ntent.ACTION_PACKAGE_ADDED進行捕獲的BroadcastReceiver的應用,首先安裝到手機上,在他接受到消息後再向你的應用返回一個廣播。你須要在你的應用中實現實現對應的BroadcastReceiver。
具體實現:
<輔助apk>
public class PackageChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
final String packageName = intent.getData().getSchemeSpecificPart();
final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
// 通知對應的應用
Intent notifyIntent = new Intent("com.app.action.notifier");
notifyIntent.setPackage(packageName);
notifyIntent.putExtra("action", action);
notifyIntent.putExtra("replace", replacing);
context.sendBroadcast(notifyIntent);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.notifier"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name="PackageChangeReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"></action>
<action android:name="android.intent.action.PACKAGE_CHANGED"></action>
<action android:name="android.intent.action.PACKAGE_REMOVED"></action>
<data android:scheme="package"></data>
</intent-filter>
</receiver>
</application>
</manifest>
<你的應用>
public class PackageChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getStringExtra("action");
boolean replace = intent.getBooleanExtra("replace", false);
if(action.equals(Intent.ACTION_PACKAGE_ADDED)){
// do some thing you want.
}
}
}
xml中註冊廣播
<receiver android:name="PackageChangeReceiver">
<intent-filter>
<action android:name="com.app.action.notifier"></action>
</intent-filter>
</receiver>
優缺點 缺點:必須先安裝輔助apk優勢:僅需一次安裝,以後使用只需在應用中實現並在XML中註冊BroadcastReceiver就能捕獲到安裝事件,從而執行相應的操做(添加桌面圖標,自啓動...)