private class PackageBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
String packageName = intent.getData().getSchemeSpecificPart();
Log.w(TAG, "onReceive() packageName : " + packageName);
if (Intent.ACTION_PACKAGE_ADDED.equalsIgnoreCase(intent.getAction())) {
Log.w(TAG, "onReceive() ACTION_PACKAGE_ADDED");
} else if (Intent.ACTION_PACKAGE_CHANGED.equalsIgnoreCase(intent.getAction())) {
Log.w(TAG, "onReceive() ACTION_PACKAGE_CHANGED");
} else if (Intent.ACTION_PACKAGE_DATA_CLEARED.equalsIgnoreCase(intent.getAction())) {
Log.w(TAG, "onReceive() ACTION_PACKAGE_DATA_CLEARED");
} else if (Intent.ACTION_PACKAGE_INSTALL.equalsIgnoreCase(intent.getAction())) {
Log.w(TAG, "onReceive() ACTION_PACKAGE_INSTALL");
} else if (Intent.ACTION_PACKAGE_REMOVED.equalsIgnoreCase(intent.getAction())) {
Log.w(TAG, "onReceive() ACTION_PACKAGE_REMOVED");
} else if (Intent.ACTION_PACKAGE_REPLACED.equalsIgnoreCase(intent.getAction())) {
Log.w(TAG, "onReceive() ACTION_PACKAGE_REPLACED");
} else if (Intent.ACTION_PACKAGE_RESTARTED.equalsIgnoreCase(intent.getAction())) {
Log.w(TAG, "onReceive() ACTION_PACKAGE_RESTARTED");
}
}
}
}
registerReceiver
mPackageBroadcastReceiver = new PackageBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
intentFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
intentFilter.addAction(Intent.ACTION_PACKAGE_DATA_CLEARED);
intentFilter.addAction(Intent.ACTION_PACKAGE_INSTALL);
intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
intentFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
intentFilter.addDataScheme("package");
registerReceiver(mPackageBroadcastReceiver, intentFilter);
unregisterReceiver
if (mPackageBroadcastReceiver != null) {
try {
unregisterReceiver(mPackageBroadcastReceiver);
mPackageBroadcastReceiver = null;
} catch (Exception e) {
e.printStackTrace();
}
}
manifest 등록
<receiver android:name=".PackageBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.ACTION_PACKAGE_ADDED" />
<action android:name="android.intent.action.ACTION_PACKAGE_CHANGED" />
<action android:name="android.intent.action.ACTION_PACKAGE_DATA_CLEARED" />
<action android:name="android.intent.action.ACTION_PACKAGE_INSTALL" />
<action android:name="android.intent.action.ACTION_PACKAGE_REMOVED" />
<action android:name="android.intent.action.ACTION_PACKAGE_REPLACED" />
<action android:name="android.intent.action.ACTION_PACKAGE_RESTARTED" />
<data android:scheme="package"/>
</intent-filter>
</receiver>
주의 : data scheme를 반드시 설정해야 한다.