IT_Programming/Android_Java

[펌] Android application 설치 완료 Broadcast 받기

JJun ™ 2014. 5. 6. 22:04

 


 출처: http://blog.naver.com/flmisj25/80189987657


 

 

1. PackageBroadcastReceiver 구현

 

  1.   private class PackageBroadcastReceiver extends BroadcastReceiver {
  2.  
  3.   @Override
  4.   public void onReceive(Context context, Intent intent) {
  5.  
  6.    if (intent != null) {
  7.     String packageName = intent.getData().getSchemeSpecificPart();
  8.     Log.w(TAG, onReceive() packageName : " + packageName);
  9.  
  10.     if (Intent.ACTION_PACKAGE_ADDED.equalsIgnoreCase(intent.getAction())) {
  11.      Log.w(TAG, onReceive() ACTION_PACKAGE_ADDED");
  12.     } else if (Intent.ACTION_PACKAGE_CHANGED.equalsIgnoreCase(intent.getAction())) {
  13.      Log.w(TAG, onReceive() ACTION_PACKAGE_CHANGED");
  14.     } else if (Intent.ACTION_PACKAGE_DATA_CLEARED.equalsIgnoreCase(intent.getAction())) {
  15.      Log.w(TAG, onReceive() ACTION_PACKAGE_DATA_CLEARED");
  16.     } else if (Intent.ACTION_PACKAGE_INSTALL.equalsIgnoreCase(intent.getAction())) {
  17.      Log.w(TAG, onReceive() ACTION_PACKAGE_INSTALL");
  18.     } else if (Intent.ACTION_PACKAGE_REMOVED.equalsIgnoreCase(intent.getAction())) {
  19.      Log.w(TAG, onReceive() ACTION_PACKAGE_REMOVED");
  20.     } else if (Intent.ACTION_PACKAGE_REPLACED.equalsIgnoreCase(intent.getAction())) {
  21.      Log.w(TAG, onReceive() ACTION_PACKAGE_REPLACED");
  22.     } else if (Intent.ACTION_PACKAGE_RESTARTED.equalsIgnoreCase(intent.getAction())) {
  23.      Log.w(TAG, onReceive() ACTION_PACKAGE_RESTARTED");
  24.     }
  25.    }
  26.   }
  27.  }

 

 

 

2. registerReceiver

 

  1.  mPackageBroadcastReceiver = new PackageBroadcastReceiver();
  2.   IntentFilter intentFilter = new IntentFilter();
  3.   intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
  4.   intentFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
  5.   intentFilter.addAction(Intent.ACTION_PACKAGE_DATA_CLEARED);
  6.   intentFilter.addAction(Intent.ACTION_PACKAGE_INSTALL);
  7.   intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
  8.   intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
  9.   intentFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
  10.   intentFilter.addDataScheme("package");
  11.   registerReceiver(mPackageBroadcastReceiver, intentFilter);

 

 

3. unregisterReceiver

  

  1.  if (mPackageBroadcastReceiver != null) {
  2.    try {
  3.     unregisterReceiver(mPackageBroadcastReceiver);
  4.     mPackageBroadcastReceiver = null;
  5.    } catch (Exception e) {
  6.     e.printStackTrace();
  7.    }
  8.   }


 

 

4. 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를 반드시 설정해야 한다.