브로드캐스트 리시버

1. 브로드캐스트 개요

시스템 브로드캐스트

예제 프로젝트


2. 브로드캐스트 리시버


2.1 Manifest에 브로드캐스트 리시버 정의하기

https://github.com/kwanulee/AndroidProgramming/blob/master/examples/BroadcastTest/app/src/main/java/com/kwanwoo/android/broadcasttest/SystemBroadcastReceiver.java


2.2 컨텍스트에 리시버 정의하기

  1. 앞의 경우와 마찬가지로
  2. SystemBroadcastReceiver 객체 생성

    BroadcastReceiver br = new SystemBroadcastReceiver();
  3. IntentFilter생성하고 registerReceiver()로 등록

    IntentFilter filter = new   IntentFilter();
    intentFilter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
    this.registerReceiver(br, filter);
  4. 등록을 취소하려면 unregisterReceiver() 호출

    public class MainActivity extends AppCompatActivity {
        BroadcastReceiver mSystemBR;
        //...
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mSystemBR = registSystemBR();
    
           //...
        }
    
        private BroadcastReceiver registSystemBR() {
            SystemBroadcastReceiver my_br = new SystemBroadcastReceiver();
            IntentFilter intentFilter_br = new IntentFilter();
            intentFilter_br.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
            intentFilter_br.addAction(Intent.ACTION_BOOT_COMPLETED);
    
            registerReceiver(my_br, intentFilter_br);
    
            return my_br;
        }
    
        @Override
        protected void onDestroy() {
            unregisterReceiver(mSystemBR);
            super.onDestroy();
        }
    }

https://github.com/kwanulee/AndroidProgramming/blob/master/examples/BroadcastTest/app/src/main/java/com/kwanwoo/android/broadcasttest/MainActivity.java


3. 배터리 상태 표시

3.1 배터리의 중요한 변화 모니터링

3.2 현재 충전상태 확인

public class BatteryWatchBR extends BroadcastReceiver {
    TextView mStatus;

    public BatteryWatchBR(TextView status) {
        mStatus = status;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        Toast.makeText(context, action, Toast.LENGTH_SHORT).show();

        IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent batteryStatus = context.registerReceiver(null, ifilter);
*       printBatteryStatus(batteryStatus);
    }

    private void printBatteryStatus(Intent intent) {
*       int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
*       int plug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED,0);
*       int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH,0);
*       int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_UNKNOWN);

        String sPlug, sStatus, sHealth;

        switch(plug) {
            case BatteryManager.BATTERY_PLUGGED_AC:
                sPlug = "AC";
                break;
            case BatteryManager.BATTERY_PLUGGED_USB:
                sPlug = "USB";
                break;
            default:
                sPlug = "Battery";
                break;
        }

         // ....
        mStatus.setText(String.format("Remain: %d \nConnection: %s\n Health: %s \n Status: %s\n ", 
                    level, sPlug, sHealth, sStatus));
    }
}

https://github.com/kwanulee/AndroidProgramming/blob/master/examples/BroadcastTest/app/src/main/java/com/kwanwoo/android/broadcasttest/BatteryWatchBR.java


SMS 수신