【问题标题】:Alarm Manager won't work when the app is killed in the background and device is locked当应用程序在后台被杀死并且设备被锁定时,警报管理器将不起作用
【发布时间】:2018-12-20 14:43:58
【问题描述】:

我在这种情况下卡了很久……

我想使用警报管理器在特定时间显示通知,现在它在以下列出的情况下工作:

  1. 当应用在后台运行时,通知将在正确的时间显示,无论设备是否被锁定。
  2. 应用在后台被杀掉后,设备未锁定时我仍然会收到正确的通知,但设备锁定时出现错误,我无法收到任何通知。

这里是 AlarmReceiver.java 的代码,所有需要的权限都已经添加到 AndroidManifest.xml 中了:

@Override
public void onReceive(Context context, Intent intent) {
    WakeLocker.acquire(context);

    String action = intent.getAction();

    Log.d(TAG, action); //when app is killed and device is locked, no info is shown at the logcat

    if (ACTION_ALARM.equals(action)) {
        Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(2 * 1000);

        notify(context, "Jello!");
    }

    WakeLocker.release();
}

public static void alarm(Context context) {
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmReceiver.class);
    intent.setAction(ACTION_ALARM);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 5 * 1000, pi);
    } else {
        alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 5 * 1000, pi);
    }
}

private void notify(Context context, String msg) {
    NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            new Intent(context, InfoActivity.class), 0);

    Notification notification =
            new NotificationCompat.Builder(context)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(context.getString(R.string.alarm))
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                    .setContentText(msg)
                    .setAutoCancel(true)
                    .setContentIntent(contentIntent).build();

    notificationManager.notify(1, notification);
}

添加的权限:

<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.VIBRATE"/>

【问题讨论】:

  • 您是否设置了正确的权限?

标签: android alarmmanager wakelock


【解决方案1】:

我刚刚找到了解决方案,将名为 FLAG_INCLUDE_STOPPED_PACKAGES 的标志设置为警报的意图,一切都会好起来的。 这是Android Developers中的插图

【讨论】:

  • 这不会有所作为,因为“这是未设置 FLAG_EXCLUDE_STOPPED_PACKAGES 时的默认行为”,正如您引用的链接所说...
【解决方案2】:

有一天我遇到了类似的问题,并在网上找到了一个解决方案,就是在广播接收器被呼叫时唤醒手机。 所以尝试创建这个类,并在调用广播接收器时调用MyWakeLock.acquire()。执行完警报后,您还应该致电MyWakeLock.release()

package your.package.name;

import android.content.Context;
import android.os.PowerManager;

public abstract class MyWakeLock {
    private static PowerManager.WakeLock wakeLock;

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

        PowerManager pm = (PowerManager) c.getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
                PowerManager.ACQUIRE_CAUSES_WAKEUP |
                PowerManager.ON_AFTER_RELEASE, MainActivity.APP_TAG);
        wakeLock.acquire();
    }

    public static void release() {
        if (wakeLock != null){
            wakeLock.release();
        }
        wakeLock = null;
    }
}

【讨论】:

  • 我已经创建了这个类并在 onReceive() 方法中获取了唤醒锁,但它仍然不起作用......我想知道当应用程序被杀死并且设备被锁定时接收器不会被触发
  • 你有权限问题
  • 我刚刚发现了另一种情况,这段代码在我的三星 Galaxy s5 上不起作用,但在 htc one x 上起作用...
  • 但是我已经添加了权限'android.permission.WAKE_LOCK'
  • @user13 不适用于 android 5.0 的三星和 4.2 的 htc
【解决方案3】:

我遇到了这个问题,最后发现错误的targetSdkVersion高于22并且没有句柄权限。 设置 targetSdkVersion 22 并工作 ^_^

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多