警报管理器未在屏幕上触发关闭

时间:2016-11-29 00:41:54

标签: android broadcastreceiver alarmmanager android-broadcast android-broadcastreceiver

我设置了报警管理器进行时间控制。我的目的是继续控制屏幕上的时间。 屏幕打开时一切正常但当我锁定设备(屏幕)我的广播接收器没有接收任何东西

我尝试了很多不同的解决方案(在清单导出:假或进程:“:远程”)关于类似的问题,但问题没有解决。我需要帮忙。我希望以前有人解决了这个问题。

用于设置闹钟管理器

    AlarmManager alarmManager = (AlarmManager)             
    context.getSystemService(Context.ALARM_SERVICE);

    Intent intent = new Intent(context, TimeService.class);
    intent.setAction(SETUP_TIMER_FOR_NOTIFICATION);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 112, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    long now = System.currentTimeMillis();
    long minute = 1000 * 60;

    alarmManager.cancel(pendingIntent);
    alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() , minute, pendingIntent);

我的收件人

public class TimeService extends WakefulBroadcastReceiver {

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



Log.i("TimeService", "onReceive");

 Intent timeIntent = new Intent(context, NotificationService.class);
        timeIntent.setAction(SETUP_TIMER_FOR_NOTIFICATION);

        startWakefulService(context, timeIntent);


   }
}

的Manifest.xml

<receiver
            android:name="service.TimeService"
            android:enabled="true"
            android:exported="false"
            >

            <intent-filter>
                <action android:name="alarm_timer_for_notification" />
            </intent-filter>
        </receiver>

2 个答案:

答案 0 :(得分:2)

setAndAllowWhileIdle当屏幕关闭且手机处于空闲模式时此功能

在棉花糖中引入打瞌睡模式以节省电池。因此在重复模式下警报未正确触发。

所以我们使用 https://developer.android.com/reference/android/app/AlarmManager.html#setAndAllowWhileIdle(int,%20long,%20android.app.PendingIntent)

从棉花糖和向上,警报仅间隔10分钟唤醒,setAndAllowWhileIdle也会在电话空闲时屏幕服务或通知10分钟(屏幕关闭)。

在正常模式下工作时间间隔正常(我检查了3分钟的间隔)

public class TimeService extends WakefulBroadcastReceiver {

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

    Log.i("TimeService", "onReceive");

    Intent timeIntent = new Intent(context, NotificationService.class);
    timeIntent.setAction(SETUP_TIMER_FOR_NOTIFICATION);

    startWakefulService(context, timeIntent);

    setAlarm(context);
}

public void setAlarm(Context context)
{
    Timber.v(TAG +"set Alarm");
    PreferencesHelper preferencesHelper =new PreferencesHelper(context);
    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, TimeService.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
    int intervalInMinutes = preferencesHelper.getInt(context.getResources().getString(R.string.sha_loc_intervals), 1)* 60000;
      if (android.os.Build.VERSION.SDK_INT >= 23) {
        alarmManager.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime()+intervalInMinutes , pendingIntent);
    } else if (android.os.Build.VERSION.SDK_INT >= 19
            && android.os.Build.VERSION.SDK_INT < 23) {
        alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+intervalInMinutes , pendingIntent);
    } else {
        alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+intervalInMinutes , pendingIntent);
    }
}

public void cancelAlarm(Context context)
{
    Timber.v(TAG +"cancel Alarm");
    Intent intent = new Intent(context, TimeService.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(pendingIntent);
}}

<receiver android:name=".TimeService"
              android:enabled="true"
              android:exported="true"
              android:process=":remote">
</receiver>

MainActivity.class

TimeService timeService = new TimeService();

timeService.setAlarm(MainActivity.this);

答案 1 :(得分:0)

您想在清单文件中获取权限。

uses-permission android:name =“android.permission.WAKE_LOCK”

WakeLock是一种保持设备开启的机制