在我的應用程序之一,我使用的應用程序(MyApplication的)的一個子類和一個BroadcastReceiver(MyBroadcastReceiver)這是添加到清單。該廣播接收器註冊的動作android.intent.action.PACKAGE_ADDED和android.intent.action.PACKAGE_REPLACED。當APK被添加到或設備上更換在Manifest中聲明的接收器是否啓動了應用程序進程和應用程序類?
這些意圖進行燒成。應用程序本身並沒有在安裝過程中顯示,所以我的問題是:應用程序的子類是否會隨應用程序進程一起啓動?
下面的代碼:
清單:
<application
...
android:name=".MyApplication" >
...
<receiver
android:name=".MyBroadcastReceiver" >
<intent-filter>
<action
android:name="android.intent.action.PACKAGE_ADDED" />
<action
android:name="android.intent.action.PACKAGE_REPLACED" />
<data
android:path="xxx.yyy.zzz"
android:scheme="package" />
</intent-filter>
</receiver>
</application>
所有MyApplication:
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Log.d(this.getClass().getName(), "onCreate");
// ...
}
@Override
public void onTerminate() {
// ...
super.onTerminate();
}
}
MyBroadcastReceiver:
public class MyBroadcastReceiver extends BroadcastReceiver {
public static final String TAG = "xxx.yyy.zzz.MyBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null && intent.getDataString() != null) {
if (intent.getDataString().contains("xxx.yyy.zzz")) {
if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
Log.d(this.getClass().getName(), "onReceive(Package added)");
// ...
} else if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) {
Log.d(this.getClass().getName(), "onReceive(Package replaced)");
// ...
}
}
}
}
}
僅供參考,您的應用程序將*自行安裝或移除時*不會收到這些廣播。 –
感謝您的澄清。 ADDED似乎是正確的,但不適用於REPLACED。這只是我的一個想法,因爲這個文本「請注意,新安裝的軟件包沒有收到這個廣播」僅提及ADDED - 不適用於REPLACED。 –