【发布时间】:2021-10-03 07:24:14
【问题描述】:
在开发一个小型 android 应用程序期间,在用户终止应用程序后,我在运行警报管理器时遇到问题。当应用在前台或后台运行时,一切正常。
我做了以下步骤:
AndroidManifest.xml
<receiver android:name="MyBroadcastReceiver" ></receiver>
MainActivity.java
在按钮的 OnClick 方法中,我调用
startAlert( x*60*1000);
x 是一个类范围的可见变量
public void startAlert(long timeInMillis){
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+(timeInMillis),pendingIntent);
}
Toast.makeText(this, "Alarm in " + x + " Minuten",Toast.LENGTH_LONG).show();
}
MyBroacastReciever.java
public void onReceive(Context context, Intent intent) {
MediaPlayer player = MediaPlayer.create(context,MainActivity.link);
player.start();
Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();}
应用关闭后,我应该怎么做才能让alarmManager成功运行?
【问题讨论】:
标签: java android broadcastreceiver alarmmanager