7

我正在做一个警报系统但是当手机关闭时我有一个问题..警报不起作用..

我将 de alarm 设置如下:

    public void doIntents(Context context, long milis, Tratam trat){
    cal=Calendar.getInstance();
    alarmManager = (AlarmManager) context.getSystemService(Service.ALARM_SERVICE);

    cal.setTimeInMillis(milis);
    Intent intent = new Intent(context, OnAlarmReceiver.class);


    pendingIntent = PendingIntent.getBroadcast(context, trat.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
    alarmManager.set(AlarmManager.RTC_WAKEUP,milis ,pendingIntent);

}

手机开机后闹钟正常..

我能做些什么?

谢谢!

4

3 回答 3

7

是的,问题是手机重新启动时您的应用程序没有运行。您需要注册一个可以接收 BOOT_COMPLETED 消息的 BroadcastReceiver,以便在手机重启时接收消息。然后在 BroadcastReceiver 中,您可以重新安排这些警报或其他什么。但我不认为你可以做任何事情来让你的闹钟在手机关机时触发..(例如让手机开机)

<receiver android:name="MyBootReceiver"
        android:enabled="true"
        android:exported="true"
        android:label="BootReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"></action>

        </intent-filter>
    </receiver>
于 2012-09-02T05:31:59.177 回答
4

当手机关闭并重新启动时,警报会被清除,但您可以使用可以接收 BOOT_COMPLETED 的 BroadcastReceiver 启动警报

In Manifest.xml:

 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  <application ...>
 <receiver android:name="com.example.receiver.AlarmMonitorReceiver"
              android:enabled="true"
              android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>
</application>

爪哇:

public class AlarmMonitorReceiver extends BroadcastReceiver{
  public void onReceive(Context context,Intent intent) { 
    if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
      AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
      Intent intentAlarm = new Intent(context, ExampleReceiver.class); 
      PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentAlarm, 0);
      Calendar time = Calendar.getInstance();
      time.setTimeInMillis(System.currentTimeMillis());
      time.add(Calendar.SECOND, 10);
      alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(),10000,pendingIntent);         
    }  

  }

}

于 2015-07-10T20:49:47.083 回答
1

您可以使用以下内容:

private val ACTION_SET_POWEROFF_ALARM = "org.codeaurora.poweroffalarm.action.SET_ALARM"
private val ACTION_CANCEL_POWEROFF_ALARM = "org.codeaurora.poweroffalarm.action.CANCEL_ALARM"
private val POWER_OFF_ALARM_PACKAGE = "com.qualcomm.qti.poweroffalarm"
private val TIME = "time"

private fun setPowerOffAlarm(context: Context, timeInMillis: Long) {
    val intent = Intent(ACTION_SET_POWEROFF_ALARM)
    intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND)
    intent.setPackage(POWER_OFF_ALARM_PACKAGE)
    val rightNow = Calendar.getInstance()

    rightNow.set(Calendar.SECOND, 0);
    rightNow.set(Calendar.MILLISECOND, 0);

    intent.putExtra(TIME, rightNow.timeInMillis + timeInMillis)

    context.sendBroadcast(intent)
    Log.i { "PWR STATE: pwr off Alarm is set" }
}

这是从位于以下位置的默认时钟应用程序的 Android 源代码复制而来的:DeskClock/src/com/android/deskclock/alarms/AlarmStateManager.java

在源代码中搜索这个函数,看看他们是如何做类似的。我们基本上会向com.qualcomm.qti.poweroffalarm包发送一个广播,然后它会像在 DeskClock 应用程序中那样创建一个关机警报。

如果我们查看反编译的源代码,com.qualcomm.qti.poweroffalarm我们会看到清单声明如下:

    <receiver android:name="com.qualcomm.qti.poweroffalarm.PowerOffAlarmDialog$ShutDownReceiver" android:permission="org.codeaurora.permission.POWER_OFF_ALARM">
        <intent-filter>
            <action android:name="org.codeaurora.poweroffalarm.action.ALARM_POWER_OFF"/>
        </intent-filter>
    </receiver>

这意味着我们还需要 qcom 包的此权限才能接受广播的意图。因此,我们将其设置在 AndroidManifest.xml 中:

<uses-permission android:name="org.codeaurora.permission.POWER_OFF_ALARM" />

然后也请求它:

    private val PERMISSION_POWER_OFF_ALARM = "org.codeaurora.permission.POWER_OFF_ALARM"

private val CODE_FOR_ALARM_PERMISSION = 1

        if (ContextCompat.checkSelfPermission(this, PERMISSION_POWER_OFF_ALARM)
        != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,
            arrayOf(PERMISSION_POWER_OFF_ALARM), CODE_FOR_ALARM_PERMISSION);
    }

最后我们要调用 SetAlarm 函数。我们指定从现在到我们希望闹钟响起的时间(以毫秒为单位)。例如在两分钟内:

 setPowerOffAlarm(context, 120000)
于 2022-01-29T15:30:28.087 回答