电池广播不发送任何状态代码

时间:2016-07-14 13:55:20

标签: android

我一直在尝试收听Battery BroadCast活动。插入/拔出。

public class BatteryReceiver extends BroadcastReceiver {
    public BatteryReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);

        if(status == BatteryManager.BATTERY_STATUS_CHARGING){
            Toast.makeText(context, "Charging", Toast.LENGTH_SHORT).show();
        } else if(status == BatteryManager.BATTERY_STATUS_DISCHARGING || status == BatteryManager.BATTERY_STATUS_NOT_CHARGING){
            Toast.makeText(context, "Not charging", Toast.LENGTH_SHORT).show();
        }
    }
}

我添加了清单操作:

<receiver
            android:name=".BatteryReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
                <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
            </intent-filter>
 </receiver>

1 个答案:

答案 0 :(得分:0)

如果您想跟踪它何时插入/拔出,我想您可以检查收到的意图。

public class BatteryReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if(intent != null) {
            String action =  intent.getAction();
            if(action != null) {
                if(action.equals("android.intent.action.ACTION_POWER_CONNECTED"){
                    Toast.makeText(context, "Charging", Toast.LENGTH_SHORT).show();
                } else if(action.equals("android.intent.action.ACTION_POWER_DISCONNECTED"){
                    Toast.makeText(context, "Not charging", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}

<强>更新

但如果你真的想检查电池是否正在充电,你必须更改你的代码。

首先,我注意到当您通过BroadcastReceiver注册AndroidManifest.xml时,收到的意图没有任何额外的内容(在日志中,当您打印意图时会出现hasExtras:

Intent { act=android.intent.action.ACTION_POWER_DISCONNECTED flg=0x4000010 cmp=com.pivoto.myapplication/.BootCompletedReceiver }
Intent { act=android.intent.action.ACTION_POWER_CONNECTED flg=0x4000010 cmp=com.pivoto.myapplication/.BootCompletedReceiver }

然后,我在StackOverflow中搜索了一下,我找到了ANSWER(请查看详细信息):

那么,有没有办法要求&#34;请求&#34;您想要的电池状态:

public class BootCompletedReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent != null) {
            Intent oneTimeOnlyBatteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
            int status = -1;
            if(oneTimeOnlyBatteryIntent != null)
                status = oneTimeOnlyBatteryIntent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
            if(status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL) {
                Toast.makeText(context, "Charging", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(context, "Not charging", Toast.LENGTH_SHORT).show();
            }
        }
    }
}