android显示手机电量
package com.basillee.asus.demo; import android.app.Notification; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.BatteryManager; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity11 extends ActionBarActivity { private TextView textViewLevel=null; private int batteryLevel; private int batteryScale; private Button buttonBatteryShow; private BroadcastReceiver broadcastReceiver=new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { //获取当前电量,如未获取具体数值,则默认为0 batteryLevel=intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0); //获取最大电量,如未获取到具体数值,则默认为100 batteryScale=intent.getIntExtra(BatteryManager.EXTRA_SCALE,100); //显示电量 textViewLevel.setText("电量"+(batteryLevel*100/batteryScale)+"%"); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_activity11); textViewLevel= (TextView) findViewById(R.id.textViewBattery); buttonBatteryShow= (Button) findViewById(R.id.button_show_battery); buttonBatteryShow.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { IntentFilter intentFilter=new IntentFilter(Intent.ACTION_BATTERY_CHANGED); //注册接收器以获取电量信息 registerReceiver(broadcastReceiver, intentFilter); } }); } }
http://jingyan.baidu.com/season/48891
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。