How to display a warning dialog box about any actions in an Android application?

In my application, I want to show a common AlertDialog for all actions.
In my background thread, server data comes in periodically.

Now, if some user criteria match this data, I want to display AlertDialog on the screen in any activity that is currently on the front panel.

How can I determine where to display the AlertDialog ?
And if my application is in the background, I want to install Notifications , not AlertDialog .

+4
source share
2 answers

You will need a service running in the background.

All necessary codes:

http://www.websmithing.com/2011/02/01/how-to-update-the-ui-in-an-android-activity-using-data-from-a-background-service/

Your service will send the broadcast to your Work. If your activity is not in the foreground, nothing will happen.

0
source

You can display a warning dialog from the background with any of the following ...

1. runOnUiThread

You need to show your dialog box in the user interface thread as shown below ...

 runOnUiThread(new Runnable() { @Override public void run() { // Your dialog code. AlertDialog ActiveCallDialog = new AlertDialog.Builder(context) .setMessage(R.string.message) .setTitle(R.string.title) .setIcon(android.R.drawable.ic_dialog_alert) .show(); } }); 

2. Handler

You can create a handler in the Activity class and call sendMessage for this handler object. Write code to display a warning in the handleMessage method of the handler, for example:

Action class

 Handler mHandler = new Handler() { public void handleMessage(Message msg) { //Display Alert AlertDialog ActiveCallDialog = new AlertDialog.Builder(context) .setMessage(R.string.message) .setTitle(R.string.title) .setIcon(android.R.drawable.ic_dialog_alert) .show(); } }); } }; 

Theme

 Thread thread= new Thread() { public void run() { //Logic mHandler.sendEmptyMessage(0); } } 
-1
source

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


All Articles