关于AlarmManager及其保存方式
About AlarmManager and the way it is saved
我正在制作一个使用警报服务的应用程序。我仍在学习它是如何工作的,但有一件事非常不清楚并且无处解释。
假设您在启动应用程序时创建了一个闹钟。警报保存在某处,因为即使您的应用不是 运行,它也需要触发,对吗?
如果是这样,我如何在重新启动我的应用程序时收到此警报,这样我就不会每次都创建一个新警报并在某处存储无限警报?
如果没有,它是如何工作的?我正在考虑使用数据库或 json 文件,但我觉得没有必要。
在我的 MainActivity class 中,我有这段代码来检查警报是否已经存在(这段代码显然是错误的)...
AlarmReceiver alarm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(alarm != null){
alarm.cancel();
}
alarm = new AlarmReceiver(MainActivity.this);
}
});
}
我已经为设备重启设置了 BroadcastReceiver(如 android 教程中所述)
public class SampleBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
new AlarmReceiver(context);
}
}
}
这是 AlarmReceiver class 本身:
public class AlarmReceiver {
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
public AlarmReceiver(Context context){
alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmBroadcastReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 17);
calendar.set(Calendar.MINUTE, 30);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 20, alarmIntent);
ComponentName receiver = new ComponentName(context, SampleBootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
public void cancel(){
alarmMgr.cancel(alarmIntent);
}
}
以及仅启动通知的 AlarmBroadcastReceiver(有效):
public class AlarmBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
new NotificationMessage(context);
}
}
The alarm is saved somewhere because it needs to trigger even when your app is not running, right?
正确。
how can I get this alarm when relaunching my app
你不知道。这是一个只写 API.
so I don't create a new one everytime and have an infinity of alarms stored somewhere?
仅在需要时创建闹钟,而不是在应用的每个 运行 上创建。
除此之外,在调用 AlarmManager
方法替换该警报(或使用 cancel()
取消警报)时,使用等效于现有警报的 PendingIntent
。
I was thinking about using a database or a json file but I have a feeling it's not necessary.
您需要在持久存储中有足够的信息,以了解闹钟响起时该怎么做。您还需要持久存储中的足够信息,以了解需要什么警报、处理重新启动、何时必须重新安排您之前安排的警报。
我正在制作一个使用警报服务的应用程序。我仍在学习它是如何工作的,但有一件事非常不清楚并且无处解释。
假设您在启动应用程序时创建了一个闹钟。警报保存在某处,因为即使您的应用不是 运行,它也需要触发,对吗?
如果是这样,我如何在重新启动我的应用程序时收到此警报,这样我就不会每次都创建一个新警报并在某处存储无限警报?
如果没有,它是如何工作的?我正在考虑使用数据库或 json 文件,但我觉得没有必要。
在我的 MainActivity class 中,我有这段代码来检查警报是否已经存在(这段代码显然是错误的)...
AlarmReceiver alarm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(alarm != null){
alarm.cancel();
}
alarm = new AlarmReceiver(MainActivity.this);
}
});
}
我已经为设备重启设置了 BroadcastReceiver(如 android 教程中所述)
public class SampleBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
new AlarmReceiver(context);
}
}
}
这是 AlarmReceiver class 本身:
public class AlarmReceiver {
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
public AlarmReceiver(Context context){
alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmBroadcastReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 17);
calendar.set(Calendar.MINUTE, 30);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 20, alarmIntent);
ComponentName receiver = new ComponentName(context, SampleBootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
public void cancel(){
alarmMgr.cancel(alarmIntent);
}
}
以及仅启动通知的 AlarmBroadcastReceiver(有效):
public class AlarmBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
new NotificationMessage(context);
}
}
The alarm is saved somewhere because it needs to trigger even when your app is not running, right?
正确。
how can I get this alarm when relaunching my app
你不知道。这是一个只写 API.
so I don't create a new one everytime and have an infinity of alarms stored somewhere?
仅在需要时创建闹钟,而不是在应用的每个 运行 上创建。
除此之外,在调用 AlarmManager
方法替换该警报(或使用 cancel()
取消警报)时,使用等效于现有警报的 PendingIntent
。
I was thinking about using a database or a json file but I have a feeling it's not necessary.
您需要在持久存储中有足够的信息,以了解闹钟响起时该怎么做。您还需要持久存储中的足够信息,以了解需要什么警报、处理重新启动、何时必须重新安排您之前安排的警报。