Application의 설치 또는 삭제 이벤트 획득하기
29 Jan 2017 | Android Event안드로이드에서 어플리케이션(이하 App)을 설치하거나 삭제할 때는 그 이벤트가 Broadcast로 전달됩니다. 즉, BroadcastReceiver를 등록해놓은 각 App들의 설치/삭제 이벤트를 수신할 수 있습니다.
AndroidManifest.xml
manifest.xml에 다음과 같이 BroadcastReceiver를 추가해줍니다.
<receiver android:name=".PackageEventReceiver"> <intent-filter> <action android:name="android.intent.action.PACKAGE_ADDED"/> <action android:name="android.intent.action.PACKAGE_REMOVED"/> <action android:name="android.intent.action.PACKAGE_REPLACED"/> <data android:scheme="package"/> </intent-filter> </receiver>
PackageEventReceiver.java
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class PackageEventReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String packageName = intent.getData().getSchemeSpecificPart(); String action = intent.getAction(); if(action.equals(Intent.ACTION_PACKAGE_ADDED)) { Log.d("", "[snowdeer] Package ADDED : " + packageName); } else if(action.equals(Intent.ACTION_PACKAGE_REMOVED)) { Log.d("", "[snowdeer] Package REMOVED : " + packageName); } } }
Event Receiver 등록
private PackageEventReceiver mPackageEventReceiver = new PackageEventReceiver(); private void registerPackageEventReceiver() { registerReceiver(mPackageEventReceiver, new IntentFilter(Intent.ACTION_PACKAGE_ADDED)); }
또는 다음과 같은 코드를 이용해서 등록하면 됩니다.
private PackageEventReceiver mPackageEventReceiver = new PackageEventReceiver(); private void registerPackageEventReceiver() { IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED); intentFilter.addAction(Intent.ACTION_PACKAGE_INSTALL); intentFilter.addDataScheme("package"); registerReceiver(mPackageEventReceiver, intentFilter); }