Android 如何在退出应用程序5分钟后开始活动?
我正在寻找一个解决方案,如何发出警报,在退出一个应用程序后,它将启动一个新的活动(如果用户选择,则为十分钟)。这将是不重复的,只有一次 我查看了TimerTask和处理程序,但它们似乎只有在应用程序位于前台时才起作用。AlarmManager看起来可以完成这项工作,但我不知道如何实现这一点。有什么建议吗 edit1: 以下是我在MainActivity中的内容:Android 如何在退出应用程序5分钟后开始活动?,android,Android,我正在寻找一个解决方案,如何发出警报,在退出一个应用程序后,它将启动一个新的活动(如果用户选择,则为十分钟)。这将是不重复的,只有一次 我查看了TimerTask和处理程序,但它们似乎只有在应用程序位于前台时才起作用。AlarmManager看起来可以完成这项工作,但我不知道如何实现这一点。有什么建议吗 edit1: 以下是我在MainActivity中的内容: Intent intent = new Intent("wake_up"); intent.setFlags(Intent.F
Intent intent = new Intent("wake_up");
intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, 5000, pendingIntent);
这是Broadcas接收器:
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent();
i.setClassName("(packagename)", "(whole class name)");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
以及我如何在清单中注册它(与其他活动一样):
我已经将Toast放入BroadcastReceiver,它可以工作,但它立即出现——将时间从5000更改为10000不会改变任何事情。报警管理器无疑是一种方法。您需要有一个广播接收器来接收警报,然后在该接收器中设置一个挂起的意图 正如你提到的,TimerTask和处理程序在这里帮不了你多少忙 使用广播接收器最简单的方法是在android清单中将其注册为广播接收器。您也可以手动注册它们,但从概念上来说这有点困难
玩得开心 Alarm manager绝对是一个不错的选择。您需要有一个广播接收器来接收警报,然后在该接收器中设置一个挂起的意图 正如你提到的,TimerTask和处理程序在这里帮不了你多少忙 使用广播接收器最简单的方法是在android清单中将其注册为广播接收器。您也可以手动注册它们,但从概念上来说这有点困难 玩得开心 在
Application
实例或主活动中的onCreate()
方法中添加此项:
Intent intent = new Intent("wake_up");
intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
PendingIntent pending = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 5000, pendingIntent);
并在广播接收器
中启动活动
:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
context.startActivity(...);
}
}
使用意向过滤器注册广播接收器
:
<receiver android:name="AlarmReceiver">
<intent-filter>
<action android:name="wake_up" />
</intent-filter>
</receiver>
在应用程序的实例或主活动中的onCreate()
方法中添加此项:
Intent intent = new Intent("wake_up");
intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
PendingIntent pending = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 5000, pendingIntent);
并在广播接收器
中启动活动
:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
context.startActivity(...);
}
}
使用意向过滤器注册广播接收器
:
<receiver android:name="AlarmReceiver">
<intent-filter>
<action android:name="wake_up" />
</intent-filter>
</receiver>
你看到PungIn意图了吗?试试这个URL,你看到了PopIn意图吗?试试这个URL可能会考虑把接收者的名字改为AlalMever,而不是蓝牙适配器,这样它就更有意义了。我已经更新了我的问题,还有一些东西丢失了。though@mklimek我将其更改为alarmManager.set(AlarmManager.Appead_REALTIME_WAKEUP,SystemClock.elapsedRealtime()+timeToCall,PendingEvent);现在工作正常:)我不知道为什么RTC_WAKEUP没有通过。P.S.Miło widziećpolski Akent na stacku;)@jeand'arme tak właśnie pomsyślałem,że zapewne到wina RTCłu醒来。Pozdrowienia:也许可以考虑把接收者的名字改为“AcRealStand”,而不是BuffoToTeAdter接收器,这样才更有意义。我已经更新了我的问题——还有一些东西丢失了。though@mklimek我将其更改为alarmManager.set(alarmManager.Appeased\u REALTIME\u WAKEUP,SystemClock.elapsedRealtime()+呼叫时间、挂起时间);现在工作很好:)我不知道为什么RTC_WAKEUP没有通过。P.S.Miło widziećpolski Akent na stacku;)@jeand'arme tak właśnie pomsyślałem,że zapewne到wina RTCłu醒来。波兹德罗维尼亚:)