Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android在接收来自待定意图的广播时出现问题_Android_Broadcastreceiver_Alarmmanager_Android Pendingintent_Wakelock - Fatal编程技术网

Android在接收来自待定意图的广播时出现问题

Android在接收来自待定意图的广播时出现问题,android,broadcastreceiver,alarmmanager,android-pendingintent,wakelock,Android,Broadcastreceiver,Alarmmanager,Android Pendingintent,Wakelock,我正在使用报警管理器在特定时间进行火灾广播。但经过多次测试,我发现有时广播接收得很晚。有时5秒,有时10秒,有时15秒甚至更长。特别是当设备锁定时。我做了各种实验。我的问题最少的代码在这里 即使使用了wake lock,我也不知道自己缺少什么 射击意图 接收广播 并在xyz活动的destroy()中释放Wakelock 自定义WakeLocker类 公共抽象类WakeLocker{ }符合: AlarmManager.setRepeating(…)自API 19起,所有重复报警 这是不准确的。如

我正在使用报警管理器在特定时间进行火灾广播。但经过多次测试,我发现有时广播接收得很晚。有时5秒,有时10秒,有时15秒甚至更长。特别是当设备锁定时。我做了各种实验。我的问题最少的代码在这里

即使使用了wake lock,我也不知道自己缺少什么

射击意图

接收广播

并在xyz活动的destroy()中释放Wakelock

自定义WakeLocker类 公共抽象类WakeLocker{

}符合:

AlarmManager.setRepeating(…)
自API 19起,所有重复报警 这是不准确的。如果您的应用程序需要精确的交付时间,那么 必须使用一次性精确报警,每次按说明重新安排 在上面targetSdkVersion早于API的旧式应用程序 19将继续保持所有警报,包括重复警报 警报,被视为精确的

这意味着您在收到Pending帐篷时必须重新设置它。
大概是这样的:

public class MyBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, Intent intent) {
    switch (intent.getAction()) {
        case AlarmHelper.ACTION_MY_ALARM: 
            doWhatYouNeed();
            long nextTime = getNextAlarmTime();
            AlarmHelper.resetAlarm(nextTime);
            break;
        ...
        }
    }
}
要获取下一个报警时间,可以使用System.currentTimeMillis()+interval或将其传递给其他用户,第二种方法更准确。 而且,我相信,你不需要在广播接收机中使用WakeLock

public class AlarmHelper {
    public static void resetAlarm(long time) { 
        Intent intent = createIntent(); 
        PendingIntent pendingIntent = createPendingIntent(intent);
        setAlarmManager(time, pendingIntent); 
    }

    public static void setAlarmManager(long time, PendingIntent pendingIntent) {
        AlarmManager alarmManager = (AlarmManager) MyApp.getAppContext().getSystemService(Context.ALARM_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, time, pendingIntent);
        } else {
            alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
        }
    }
}

你在用安卓棒棒糖吗?看看这个:
private static PowerManager.WakeLock wakeLock;

public static void acquire(Context ctx) {
    if (wakeLock != null) wakeLock.release();

    PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
            PowerManager.ACQUIRE_CAUSES_WAKEUP |
            PowerManager.ON_AFTER_RELEASE, "haris");
    wakeLock.acquire();
}

public static void release() {
    if (wakeLock != null) wakeLock.release(); wakeLock = null;
}
public class MyBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, Intent intent) {
    switch (intent.getAction()) {
        case AlarmHelper.ACTION_MY_ALARM: 
            doWhatYouNeed();
            long nextTime = getNextAlarmTime();
            AlarmHelper.resetAlarm(nextTime);
            break;
        ...
        }
    }
}
public class AlarmHelper {
    public static void resetAlarm(long time) { 
        Intent intent = createIntent(); 
        PendingIntent pendingIntent = createPendingIntent(intent);
        setAlarmManager(time, pendingIntent); 
    }

    public static void setAlarmManager(long time, PendingIntent pendingIntent) {
        AlarmManager alarmManager = (AlarmManager) MyApp.getAppContext().getSystemService(Context.ALARM_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, time, pendingIntent);
        } else {
            alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
        }
    }
}