본문 바로가기

Programming/Android

BroadcastReceiver

반응형
BroadcastReceiver 예제입니다.

앱이 추가되고 삭제될때 receive 하는 예제입니다.
package com.yhg.sample;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class PackageReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent itt) {
		String packageName = itt.getData().getSchemeSpecificPart();
		String action = itt.getAction();
		
		if(action.equals(Intent.ACTION_PACKAGE_ADDED)){
			Log.d("ADDED",packageName);
		}
		else if(action.equals(Intent.ACTION_PACKAGE_REMOVED)){
			Log.d("REMOVED",packageName);
		}
	}

}

package com.yhg.sample;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

public class SampleActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        this.registerReceiver(new PackageReceiver(), new IntentFilter(Intent.ACTION_PACKAGE_ADDED));
    }
}




    

    
        
            
                

                
            
        
        
        
            
                
                
                
            
        
    




 
 
반응형