繁体   English   中英

在广播接收器的 AlarmManager 事件上启动 Activity

[英]Launch Activity on AlarmManager event from broadcast receiver

我有一个警报管理器,通过它我通过提供广播接收器设置触发它的时间,广播接收器调用的onReceive方法成功但是知道如果应用程序没有在前台运行,如何从onReceive启动应用程序?

清单

<receiver
    android:exported="true"
    android:name=".AlarmReceiver" />

BroadcastReceiver

  @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);
    }

主要活动

        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);

请帮助谢谢

它基本上是一个使用 exoplayer 的广播音乐 stream,我们需要的是用户设置他想要播放的时间,当那个时间到来时 stream 会自动开始播放。

后台服务在 Android 8.0+ 上只会运行一分钟。 对于音乐播放器,您需要一个前台服务,其中关联的Notification为用户提供播放控制(例如,暂停、继续、停止)。

首先,你应该使用workManager此外,Brodcastreciver 对不同的 android 版本文档有限制

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM