【问题标题】:Problems with android alarm manager, when the app is closedandroid警报管理器的问题,当应用程序关闭时
【发布时间】: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


    【解决方案1】:

    您可以将警报管理器放入服务中。即使用户退出了主应用程序,前台服务也用于保持我们的应用程序处于活动状态。然后警报管理器可以触发广播接收器类中的代码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-05
      • 1970-01-01
      • 1970-01-01
      • 2017-04-25
      • 1970-01-01
      相关资源
      最近更新 更多