android - 当屏幕/CPU 关闭时,广播接收器 onReceive 方法不会被调用

标签 android android-broadcast

我正在开发一个应用程序,它会通知我(通过播放铃声)电池电量已达到一定水平。级别是可配置的。为此,我创建了一个启动服务的 Activity ,该服务反过来为 ACTION_BATTERY_CHANGED 注册接收器。

MyActivity -> MyService -> MyBrodcastReceiver [ACTION_BATTERY_CHANGED] -> onReceive() -> if(电池电量 <= MyValue) -> 播放铃声

只要屏幕打开,一切都可以正常工作,但一旦手机锁定并且屏幕关闭或 CPU 休眠,广播接收器的 onReceive 方法就不会被调用,当我再次解锁手机时,一切都会恢复正常作品。我通过日志记录验证了这一点。

ACTION_BATTERY_CHANGEDonReceive 方法是否仅在手机屏幕打开时调用,并在手机 sleep 时停止?

我什至尝试在 onReceive 方法中使用唤醒锁,但这不起作用 

[我正在使用 ICS (4.0.4) 进行测试]

import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;


public class BatteryMeterService extends Service {

    private BatteryStatusReceiver batteryStatusReceiver;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        batteryStatusReceiver = new BatteryStatusReceiver(null); 
        registerReceiver(batteryStatusReceiver, intentFilter);      
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(batteryStatusReceiver);
    }

}


import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.BatteryManager;
import android.os.PowerManager;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.util.Log;
import android.widget.TextView;

import com.amol.bm.BatteryMeterUtility.NotificationInfo;

public class BatteryStatusReceiver extends BroadcastReceiver {


    private BatteryMeterUtility batteryMeterUtility;

    public BatteryStatusReceiver() {
        super();
    }

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


        int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
        float fPct = (level / (float)scale) * 100;
        int levelPct = (int)fPct; 

        boolean prefAlertLowBattery = sharedPrefs.getBoolean("prefAlertLowBattery", true);
        if(prefAlertLowBattery) {
                String prefAlertLowBatteryValue = sharedPrefs.getString("prefAlertLowBatteryValue", "20");
                int lowBatteryValue = Integer.parseInt(prefAlertLowBatteryValue);
                if(levelPct <= lowBatteryValue && iStatus != BatteryManager.BATTERY_STATUS_CHARGING) {
                notificationInfo.icon = R.drawable.low_battery;
                PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
                PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "BM WakeLook");
                wakeLock.acquire();
                batteryMeterUtility.playAlertRingtone(alertRingtone);
                wakeLock.release();
                }
        }
    }

}

最佳答案

您应该向在后台运行的服务授予 WAKE_LOCK 权限,这样即使手机空闲或关机,您的服务也能继续运行。希望你明白了,如果不清楚请告诉我

关于android - 当屏幕/CPU 关闭时,广播接收器 onReceive 方法不会被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16411362/

相关文章:

android - 带有一个输入edittext的计算器android

android - 在不创建 Activity 实例的情况下从 BroadCastReceiver 类调用 Activity 的方法

android - SecurityException : Permission Denial: not allowed to send broadcast android. intent.action.BATTERY_CHANGED

android - 所有设备上均未收到 BOOT_COMPLETED Intent

android - 如何在我的移动浏览器上打开位于本地服务器上的资源?

java - 启动时未调用内容观察者的 Onchange

android - 使用 putString 添加到 SharedPreferences 文件时是否重复?

java - Libgdx 强制垃圾收集器

android - 关闭 GPS 时广播接收器被调用 2 次?

java - 我如何知道我的服务何时从广播接收器与 Android 调用?