How to raise an alert dialog from BroadcastReceiver class?

前端 未结 3 852
孤街浪徒
孤街浪徒 2020-12-18 08:29

I have used a timer method in an Activity class. In that method I have an intent from Activity class to a BroadcastReceiver class.

3条回答
  •  悲哀的现实
    2020-12-18 09:22

    If your activity is running when the BroadcastReceiver gets the intent you should be able to use runOnUiThread to run a method that creates an AlertDialog, e.g.:

    public void onReceive(Context context, Intent intent)
    {
        runOnUiThread(new Runnable() {
            public void run() {
                AlertDialog.Builder d = new AlertDialog.Builder(MyActivity.this);
                b.setMessage("This is a dialog from within a BroadcastReceiver");
                b.create().show();
            }
        });
    
    }
    

    This works if you make your BroadcastReceiver an inner class to your Activity.

提交回复
热议问题