【问题标题】:Pop up window after the alarm signal - Android报警信号后弹出窗口 - Android
【发布时间】:2018-05-14 19:48:29
【问题描述】:

我想知道 - 当AlarmManager 调用时如何显示弹出窗口?我已经创建了AlarmManager,现在我需要创建一些会显示弹出窗口以取消此警报的内容。

我的代码:

public void setAlarm(long timeInMillis){

    if(Build.VERSION.SDK_INT >= 23){
        mCalendar.set(
                mCalendar.get(Calendar.MONTH),
                mCalendar.get(Calendar.YEAR),
                mCalendar.get(Calendar.DAY_OF_YEAR),
                mCalendar.get(Calendar.HOUR_OF_DAY),
                mCalendar.get(Calendar.MINUTE)
        );
    }

    final AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    Intent intent = new Intent(this, MyAlarm.class);
    intent.setData(currentUri);


    final PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);


    alarmManager.setExact(AlarmManager.RTC, timeInMillis, pendingIntent);
}

public class MyAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    MediaPlayer mediaPlayer = MediaPlayer.create(context, Settings.System.DEFAULT_RINGTONE_URI);


    mediaPlayer.start();

}

}

【问题讨论】:

  • 弹出窗口?你的意思是说警报对话框?
  • 是的,我想创建一个警报对话框来取消呼叫警报

标签: java android alarmmanager popupwindow


【解决方案1】:
onReceive(context, intent) {
  /*show the dialog in this method. set the onclick so it can dismiss the alarm, 
    get the value for the alarm from the bundle. I may be wrong about this 
    but i think alarmManager has a cancel(PendingIntent operation) method that u can 
    just send in the intent and your done. 
    Call a stopMedia(context) method after the cancel in order to stop the media 
    that is playing
   */
   showDialog(context, intent)
   //Extract the play media code to a method for readability
   playMedia(context) 
}

应该可以解决你的问题


代码发布之前:

我们可以使用待处理的 Intent 并拥有一个处理待处理 Intent 的 Activity。或者使用处理程序来执行代码。

在任何一种情况下,创建一个对话框片段,然后使用适当的上下文来显示它。对话框片段按钮的 setOnClickListener { alarmManager.cancel }。

可能需要更多解释,具体取决于警报管理器的设置方式

【讨论】:

  • 好的,谢谢,我会试试的。我的问题是我需要创建具有选定时间的 AlertDialog,我的意思是及时转发到 AlarmManager,因为正如我之前写的那样,我想获得一个“弹出”窗口,该窗口将在警报调用时显示。你认为你的解决方案会好吗?
  • 您应该将发送到alarmManager 的时间保存在本地某处,以便alertDialog 可以访问它。如果不看代码,我很难给你正确的指示。
  • 好的,我编辑了帖子并添加了我的代码片段。让我们检查一下
  • 编辑了响应以显示您可能使用的代码。
  • 是的,只是我不确定如何将它添加到我的代码中,但我会尝试。如果我成功了,我会在这里写。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-05
  • 1970-01-01
  • 2014-06-02
  • 1970-01-01
  • 2019-05-15
相关资源
最近更新 更多