Broadcast Receiver

public class BatteryReceiver extends Activity {
    private TextView bl;
    private BroadcastReceiver br = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
                int level = intent.getIntExtra("level", 0);
                int scale = intent.getIntExtra("scale", 100);
                bl.setText("BatteryLevel:" + level / scale * 100 + "%");
            }
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_batter_receiver);
        bl = (TextView) findViewById(R.id.bl);
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        unregisterReceiver(br);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        registerReceiver(br, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    }

}

 

posted @ 2012-12-06 10:36  Mr.haox  阅读(305)  评论(0编辑  收藏  举报