How to raise warning dialog from BroadcastReceiver class?

I used the timer method in the Activity class. In this method, I have an intent from the Activity class to the BroadcastReceiver class.

This BroadcastReceiver class will call every 15 minutes in the background using the AlarmManager .

When I call the BroadcastReceiver class, I would like to raise the AlertDialog .

 public void timerMethod(){ Intent intent = new Intent(Activity.this, BroadcastReceiverClass.class ); PendingIntent sender = PendingIntent.getBroadcast( QualityCallActivity.this,0, intent, 0 ); // We want the alarm to go off 30 seconds from now. long firstTime = SystemClock.elapsedRealtime(); AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 60*1000, sender); } 

BroadcastReceiverClass.java

 public void onReceive(Context context, Intent intent) { dialogMethod(); } 

How can I raise the AlertDialog class from BroadcastReceiver from the background process?

+4
source share
3 answers

In short: maybe not .

Only activity can create / show dialogs. In fact, this has been set more than once:

Also, why do you want to start a dialog?

  • If the user is not in your application (let's say he plays the Game) and your Dialog appears every 15 minutes, it will be very annoying to him.
  • If the user is in your application, there are several other (better suited) ways to tell him that something has been completed.

More convenient ways

In fact, you can create / show Toast from BroadcastReceiver . This Toast will also be shown when the user is not "in your application."

In addition, you can send a notification (shown in the notification bar at the top of the screen) with BroadcastReceiver . A tutorial on how to do this (it is no different than how you do it in Activity, except that you use the passed Context Object from the onReceive method).

A notification will also be displayed when the user is not "in your application", and this (in my opinion) is the best solution to this problem.

+5
source

If your activity is started when the BroadcastReceiver receives the intent, you should use runOnUiThread to run the method that creates the AlertDialog, for example:

 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 for your activity.

+7
source

1) In action:

 public static Context ctx; onCreate { ctx = this; } public void showAlertDialog(Context context, String title, String message) { final AlertDialog alertDialog = new AlertDialog.Builder(context).create(); // Setting Dialog Title alertDialog.setTitle(title); // Setting Dialog Message alertDialog.setMessage(message); // Setting OK Button alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Okay", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { alertDialog.dismiss(); } }); // Showing Alert Message alertDialog.show(); } 

2) In BroadcastReceiver.onReceive :

 YourActivity ac= new YourActivity (); ac.showAlertDialog(YourActivity.ctx, "test", "test"); 
0
source

Source: https://habr.com/ru/post/1369479/


All Articles