当前位置:操作系统 > 安卓/Android >>

接收系统广播消息之监听手机电量变化

当手机电量发生改变时,系统会对外发送Intent的Action为android.intent.action.BATTERY_CHANGED常量的广播;当手机电量过低时,系统会对外发送Intent的Action为android.intent.action.BATTERY_LOW常量的广播。

当手机电池从电量不足状态恢复时,系统会对外发送Intent的Action为android.intent.action.BATTERY_OKAY常量的广播。

下面通过一个简单实例来演示:

Receiver:


 

package com.home.receiver; 
 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.widget.Toast; 
 
public class BatteryReceiver extends BroadcastReceiver { 
 
    @Override 
    public void onReceive(Context context, Intent intent) { 
        if (Intent.ACTION_BATTERY_OKAY.equals(intent.getAction())) { 
            Toast.makeText(context, "电量已恢复,可以使用!", Toast.LENGTH_LONG).show(); 
        } 
        if (Intent.ACTION_BATTERY_LOW.equals(intent.getAction())) { 
            Toast.makeText(context, "电量过低,请尽快充电!", Toast.LENGTH_LONG).show(); 
        } 
        if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) { 
            Bundle bundle = intent.getExtras(); 
            // 获取当前电量  
            int current = bundle.getInt("level"); 
            // 获取总电量  
            int total = bundle.getInt("scale"); 
            StringBuffer sb = new StringBuffer(); 
            sb.append("当前电量为:" + current * 100 / total + "%" + "  "); 
            // 如果当前电量小于总电量的15%  
            if (current * 1.0 / total < 0.15) { 
                sb.append("电量过低,请尽快充电!"); 
            } else { 
                sb.append("电量足够,请放心使用!"); 
            } 
            Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show(); 
        } 
 
    } 
 
} 

package com.home.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class BatteryReceiver extends BroadcastReceiver {

 @Override
 public void onReceive(Context context, Intent intent) {
  if (Intent.ACTION_BATTERY_OKAY.equals(intent.getAction())) {
   Toast.makeText(context, "电量已恢复,可以使用!", Toast.LENGTH_LONG).show();
  }
  if (Intent.ACTION_BATTERY_LOW.equals(intent.getAction())) {
   Toast.makeText(context, "电量过低,请尽快充电!", Toast.LENGTH_LONG).show();
  }
  if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
   Bundle bundle = intent.getExtras();
   // 获取当前电量
   int current = bundle.getInt("level");
   // 获取总电量
   int total = bundle.getInt("scale");
   StringBuffer sb = new StringBuffer();
   sb.append("当前电量为:" + current * 100 / total + "%" + "  ");
   // 如果当前电量小于总电量的15%
   if (current * 1.0 / total < 0.15) {
    sb.append("电量过低,请尽快充电!");
   } else {
    sb.append("电量足够,请放心使用!");
   }
   Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();
  }

 }

}

配置Receiver:

receiver android:name="com.home.receiver.BatteryReceiver"> 
          <intent-filter> 
              <action android:name="android.intent.action.BATTERY_CHANGED" /> 
              <action android:name="android.intent.action.BATTERY_OKAY"/> 
              <action android:name="android.intent.action.BATTERY_LOW"/> 
          </intent-filter>  
       </receiver> 

 <receiver android:name="com.home.receiver.BatteryReceiver">
           <intent-filter>
               <action android:name="android.intent.action.BATTERY_CHANGED" />
               <action android:name="android.intent.action.BATTERY_OKAY"/>
               <action android:name="android.intent.action.BATTERY_LOW"/>
           </intent-filter>
        </receiver>

 

 

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,