android 手机判断是否在充电 如断电自动打电话提醒功能

public class MainActivity extends Activity
{
    TextView myText;
    private BroadcastReceiver mbatteryReceiver=new BroadcastReceiver()
    {
        public void onReceive(Context context, Intent intent)
        {
            String action =intent.getAction();
            if(Intent.ACTION_BATTERY_CHANGED.equals(action));
            {
                int status=intent.getIntExtra("status",BatteryManager.BATTERY_STATUS_UNKNOWN);
                if(status==BatteryManager.BATTERY_STATUS_CHARGING)
                {
                    myText.setText("充电ing");
                    Intent i =new Intent(Intent.ACTION_CALL,Uri.parse("tel:111511"));
                    MainActivity.this.startActivity(i);
                }
                else
                {
                    myText.setText("断电ing");
                }
            }
        }
    };
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myText=(TextView)findViewById(R.id.message);
       
        registerReceiver(mbatteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    }
}

猜你喜欢

转载自yangsongjing.iteye.com/blog/2212110