如何在设备启动时使用 AlarmManager 启动警报?
How to start an alarm when the device boots with AlarmManager?
我正在尝试使用 AlarmManager
设置一个闹钟来删除我的应用保存在文件夹中的文件,从最旧的文件开始,一次只删除一定数量的文件。我一直在阅读 this link,但我曾一度感到困惑。
设备启动时启动警报下的步骤 2:
public class SampleBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
// Set the alarm here.
}
}
}
现在,代码表示 //Set the alarm here.
这就是我感到困惑的地方。我应该更改哪些部分以适应我的具体情况?
另外,我不知道该把这段代码放在我的应用程序中的什么位置。我很确定它会在我的 AndroidManifest
中,但在 <application
部分或在其自己的类别下或什么?
截图会很有帮助。谢谢你。我正在使用 Android Studio 2.2.2,我的应用程序适用于 Android 4.4.
在您的例子中,扩展 BroadcastReceiver
的 SampleBootReceiver
class 位于单独的 class 文件中。每当启动 phone 时,它都会 运行 onReceive()
方法中的任何代码,只要您的清单中也有 intent 过滤器,就像这样:
<receiver
android:name=".SampleBootReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
然后您将想要开始您的 AlarmManager
。您可以在 BroadcastReceiver
的 onReceive()
方法中执行此操作,或者在从 BroadcastReceiver
.
启动的单独服务中执行此操作
启动闹钟:
// Make the intent to fire NewReciever
Intent intent= new Intent(getApplicationContext(), NewReceiver.class);
// Make PendingIntent to be triggered each time the alarm goes off
final PendingIntent pIntent = PendingIntent.getBroadcast(this, 0,
intentDayAlarmStart, 0);
//setup calendar object for alarm start time
Calendar cal= Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.HOUR_OF_DAY, hourToStart);
cal.set(Calendar.MINUTE, minuteToStart);
cal.set(Calendar.SECOND, 0);
//make the alarm
AlarmManager morningAlarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
//(type, starttime, interval, pintent)
morningAlarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, dailyCalendar.getTimeInMillis(),
AlarmManager.INTERVAL_FIFTEEN_MINUTES, pIntent);
此警报将在 hourToStart
和 minuteToStart
设置的任何时间开始每隔 15 分钟触发另一个 BroadcastReceiver
、NewReceiver
。也只需在您的清单中设置新的接收器!
我正在尝试使用 AlarmManager
设置一个闹钟来删除我的应用保存在文件夹中的文件,从最旧的文件开始,一次只删除一定数量的文件。我一直在阅读 this link,但我曾一度感到困惑。
设备启动时启动警报下的步骤 2:
public class SampleBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
// Set the alarm here.
}
}
}
现在,代码表示 //Set the alarm here.
这就是我感到困惑的地方。我应该更改哪些部分以适应我的具体情况?
另外,我不知道该把这段代码放在我的应用程序中的什么位置。我很确定它会在我的 AndroidManifest
中,但在 <application
部分或在其自己的类别下或什么?
截图会很有帮助。谢谢你。我正在使用 Android Studio 2.2.2,我的应用程序适用于 Android 4.4.
在您的例子中,扩展 BroadcastReceiver
的 SampleBootReceiver
class 位于单独的 class 文件中。每当启动 phone 时,它都会 运行 onReceive()
方法中的任何代码,只要您的清单中也有 intent 过滤器,就像这样:
<receiver
android:name=".SampleBootReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
然后您将想要开始您的 AlarmManager
。您可以在 BroadcastReceiver
的 onReceive()
方法中执行此操作,或者在从 BroadcastReceiver
.
启动闹钟:
// Make the intent to fire NewReciever
Intent intent= new Intent(getApplicationContext(), NewReceiver.class);
// Make PendingIntent to be triggered each time the alarm goes off
final PendingIntent pIntent = PendingIntent.getBroadcast(this, 0,
intentDayAlarmStart, 0);
//setup calendar object for alarm start time
Calendar cal= Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.HOUR_OF_DAY, hourToStart);
cal.set(Calendar.MINUTE, minuteToStart);
cal.set(Calendar.SECOND, 0);
//make the alarm
AlarmManager morningAlarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
//(type, starttime, interval, pintent)
morningAlarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, dailyCalendar.getTimeInMillis(),
AlarmManager.INTERVAL_FIFTEEN_MINUTES, pIntent);
此警报将在 hourToStart
和 minuteToStart
设置的任何时间开始每隔 15 分钟触发另一个 BroadcastReceiver
、NewReceiver
。也只需在您的清单中设置新的接收器!