是否可以从 BroadcastReceiver 启动警报对话框?

Is is possible Launch Alert Dialog from BroadcastReceiver?

我正在创建一个闹钟应用程序,在指定的时间,AlarmBroadcast 启动。 我要在指定时间加Alert Dialog

这就是我所做的。

public class AlarmBrodcast extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {


        final AlertDialog alertDialog = new AlertDialog.Builder(context.getApplicationContext()).create();
        alertDialog.setTitle("Delete Remainder");
        alertDialog.setMessage("Are you sure you want to Delete this Remainder");
        alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                alertDialog.dismiss();

            }
        });


        alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                alertDialog.dismiss();
            }
        });


        alertDialog.show();

这给了我以下错误信息。

java.lang.RuntimeException: Unable to instantiate receiver com.example.taha.alarmproject.AlarmBrodcast: java.lang.ClassCastException: com.example.taha.alarmproject.AlarmBrodcast cannot be cast to android.content.BroadcastReceiver

编辑 MainActivity

      Intent intent = new Intent(this, AlarmBrodcast.class);
        intent.putExtra("message", "Alarm Message 00001");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                this.getApplicationContext(), 234324243, intent, 0);

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
 /*       alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);*/


        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
                + (i * 1000), pendingIntent);


        Toast.makeText(this, "Alarm set in " + i + " seconds", Toast.LENGTH_LONG).show();

我还尝试从 BrodcastReceiver 启动 Activity,但它也未能启动。

一个简单的解决方案是使用 Event。这就是我在我的项目中使用的一个漂亮的小图书馆的做法:

compile 'org.greenrobot:eventbus:3.0.0'

将该行添加到您的 build.gradle(模块级)文件中。

这里的想法是让你的AlarmBroadcast在调用onReceive时通知一些Activityclass。

创建一个 Plain Old Java Object (POJO) class 来代表您的活动!

public class BroadCastEvent{
  private boolean isCompleted;

  BroadCastEvent(boolean completed){
     this.isCompleted = completed;
  }

  //getter
  public boolean isCompleted(){
     return this.isCompleted;
  }
}

现在,在 AlarmBroadcast class 的 onReceive 方法中:

EventBus.getDefault().post(new BroadCastEvent(true));

接下来,在您的 activity 中,像这样注册以收听此事件:

EventBus.getDefault().register(this);

然后覆盖这个方法:

public void onEvent(BroadCastEvent event){
   if(event.isCompleted()){
      //show your dialog here or start next activity
   }
}

接下来,在 onDestroy 方法中注销事件总线:

@Override
public void onDestroy(){
   super.onDestroy();

   EventBus.getDefault().unregister(this);
}

这会解耦您的代码并允许您将 AlarmBroadcast class 设为 Publisher 并将 activity 设为 Subscriber!

希望对您有所帮助,请告诉我进展如何!