从未收到警报

问题描述 投票:0回答:1

我们的平板电脑应该一直处于唤醒状态,但是电源按钮在意外事故中被击中。解决方案似乎是在设备进入锁定/睡眠状态后约5秒设置警报。我似乎无法弄清楚如何接收此警报。

我已经看过(似乎)这里的所有线程,但是我一定缺少一些东西。我declared a receiver in the manifest fileimplemented the receiver,但从不触发。

正在运行adb shell dumpsys警报,它在主要警报下列出:

+32ms running, 2 wakeups, 2 alarms: u0a1:com.android.providers.calendar
      *walarm*:io.myapp/.sysutils.Alarm

并且也在IDLE下列出:

u0a20:io.myapp +286ms running, 1 wakeups:
    +286ms 1 wakes 1 alarms: *walarm*:io.myapp/.sysutils.Alarm

正在运行日志消息“ setAlarm”,并且adb也正在显示它。为什么我不能收到它?

AndroidManifest.xml

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

Alarm.java

public class Alarm extends BroadcastReceiver {
    private String ID = "WAKE_SLEEP_ALARM";
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Log.d("Alarm", "onReceive");
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "alarm:wl");
        wl.acquire();

        // Put here YOUR code.
        Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example

        wl.release();
    }

    public void setAlarm(Context context)
    {
        Log.d("Alarm", "setAlarm");
        AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(context, Alarm.class);
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
        am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, pi);
    }
}

我还尝试了其他一些变体,包括添加一个Intent过滤器:

<receiver android:process=":remote" android:name=".sysutils.Alarm"
            android:enabled="true"
            android:exported="true"
            >
            <intent-filter>
                <action android:name="CUSTOM_ALARM" >
                </action>
            </intent-filter>
        </receiver>

并更改我的意图:

Intent i = new Intent("CUSTOM_ALARM");

但是这些都不起作用。

我正在与应用程序上下文一起发送警报(setAlarm)。

我正在运行android Lolipop(制造商的不幸要求)API 15

java android alarmmanager alarm
1个回答
0
投票

代替调用am.set(...,使用am.setAlarmClock(...

© www.soinside.com 2019 - 2024. All rights reserved.