【发布时间】:2021-11-29 04:56:17
【问题描述】:
我有一个警报管理器,通过它我通过提供广播接收器设置触发它的时间,onReceive 广播接收器方法成功调用,但是如果应用程序没有运行,任何想法如何从onReceive 启动应用程序前景?
菜单
<receiver
android:exported="true"
android:name=".AlarmReceiver" />
广播接收器
@Override
public void onReceive(Context context, Intent intent)
{
Intent scheduledIntent = new Intent(context, MainActivity.class);
scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(scheduledIntent);
}
MainActivity
AlarmManager alarmManager=(AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent=new Intent(this,AlarmReceiver.class);
PendingIntent pendingIntent=PendingIntent.getBroadcast(this,1000,intent,0);
alarmManager.set(AlarmManager.RTC_WAKEUP,timeInMillis,pendingIntent);
请帮助 谢谢
【问题讨论】:
-
您无法在现代版本的 Android 上从后台启动活动。请改用高优先级的
Notification。 -
感谢@CommonsWare 的及时回复,如果我想自动播放预定的音乐流,您建议使用什么?其次,正如您建议使用高优先级通知,它是本地通知还是推送通知,我可以从推送通知启动活动吗?谢谢
-
"如果我想自动播放预定的音乐流,您建议使用什么?" -- 如果它类似于铃声,您或许可以将其直接绑定到
Notification。如果它更长一些,让Notification为播放流的前景Service提供UI。 “是本地通知还是推送通知”——Android 上的Notification指的是我认为您所说的“本地通知”。 “我可以从推送通知中启动活动吗?” -- 没有。 -
它基本上是一个使用 exoplayer 的广播音乐流,我们需要的是用户设置一个他想要播放的时间,当那个时间到来时流自动开始播放。我脑海中的两件事是后台服务或前台服务。
标签: android android-activity broadcastreceiver alarmmanager