Android手机如何判断已经充电完成?

2022-08-13 社会 69阅读
  • android提供的API是BatteryManager类,里面仅提供电量等级与当前电压的参数,并且直接提供了一个电量满的状态码.

  • 下面是一个例子:

    • 显示充电状态的例子

    • 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("充电中");
       
                      }
                      else
                      {
                          myText.setText("断电");
                      }
                  }
              }
          };
         
          @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));
          }

声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com