(在将此问题标记为重复之前,请注意,我在特定 API 级别 (API R) 中遇到此问题,该应用在低于 R 的其他 API 级别上显示正常的预期工作行为)。嗯>
在 Pixed 3a 上运行应用程序时 API R
我正在通过构建一个基本的警报应用程序来尝试 Android 中的警报管理器。我有一个 AlarmPageActivity,我试图从我的自定义广播接收器( AlarmReceiver.java
)启动
我的 MainActivity 中有代码,我使用警报管理器设置警报。当警报响起时,待处理的 Intent 将转到 AlarmReceiver
我尝试使用 context.startActivity(context, alarmIntent)
开始一项 Activity 的类(class).
问题: [在你去那里之前,是的,我有所需的适当标志 ( Intent.FLAG_ACTIVITY_NEW_TASK
)]。当我设置闹钟时间并保持应用程序打开时,AlarmPageActivity(闹钟响起时应该弹出打开的 Activity )启动并且应用程序执行正常行为。但是,当我设置时间并关闭应用程序 ( onDestroy()
) 时,代码将继续执行 AlarmReceiver.java
(我的广播接收器),铃声开始播放,但我的 AlarmPageActivity 没有显示(事实上,即使是 onCreate()
方法也不会执行)。它也不会在日志/运行窗口中显示任何错误。我尝试更改所有内容,从挂起的 Intent 参数到向 Intent 对象添加新的 Activity 标志等,但是没有任何效果!您可以在下面的代码中看到,没有语法错误,应该阻止代码通过广播接收器启动新 Activity (当用户位于应用程序外部时)。
我遇到这个问题将近两天了。我阅读了互联网上关于从广播接收器类开始 Activity 的每一篇文章/帖子。但这是行不通的,我感到非常沮丧。然后奇迹般,我想为什么不尝试在不同的模拟器上运行该应用程序。你猜怎么着? 成功了。 sigh...
我一直认为我做错了什么或者我的代码很糟糕,因为我在 android studio 中只是初学者。但我几乎不知道,我的代码一直都是正确的。
在 Pixed 3a 上运行应用程序时 API 26
该应用程序显示正常的预期行为。即使在 onDestroy() 之后,BroadCast 接收器也会接收并启动 AlarmPageActivity,带有铃声 :)
我的问题:这是相同的代码(我没有更改/添加任何其他内容),该代码在运行 API R 的模拟器设备上不起作用,但现在可以在 API 26 中运行。有人可以吗?解释一下为什么它不起作用?我真的很感激。或者也许我的代码在某种程度上与 API R 不兼容......谁知道呢?赐教一下!
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
...
other code...
...
// Alarm manager code ----------------------------------------
alarmMgr = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 56);
calendar.set(Calendar.SECOND, 0);
long startTime = calendar.getTimeInMillis();
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
final PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(this, 0,
alarmIntent, 0);
alarmMgr.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, startTime, alarmPendingIntent);
}
AlarmReceiver.java
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent){
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Log.d("asdfasdf", "BroadcastReceiver (alarmReceiver) activity reached");
Intent alarmIntent = new Intent(context.getApplicationContext(), AlarmPageActivity.class);
alarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(alarmIntent);
alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
// ringtone code ...
ringtone.play();
Log.d("asdfasdf", "Reached end of alarmReceiver ");
}
} // mind the wrong indenting here
AlarmPageActivity.java
has the default empty activity code...
是的,我知道我不应该从广播接收器运行 Activity ,因为它不应该执行需要 > 10 秒的繁重任务以及所有操作,但我只是想看看它会像以前一样稍后改进该代码。
最佳答案
我认为它不适用于 Android 10 及更高版本。请阅读有关 Restrictions on starting activities from the background 的更多详细信息。尝试针对此案例请求权限 SYSTEM_ALERT_WINDOW
关于java - 无法从 API R 中的 BroadcastReceiver 类启动Activity(上下文, Intent )(android studio),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61949102/