Scenario: Open thread download upgrade package in Service. When the system upgrade package is downloaded, a dialog pops up to prompt the user.
Note that the Android system version is different and may have different performances. Currently, the service based on Android 8.1.0 was shot in Dialog.
First, you must declare permissions in the function list list, and both of the following must be declared:
<uses-permission android:name=".SYSTEM_ALERT_WINDOW"/><!--This line of code must exist,Otherwise, the button in the system settings cannot be clicked--> <uses-permission android:name=".SYSTEM_OVERLAY_WINDOW" />
Then, when MainActivity is initialized, you must check again whether the current application is allowed to be displayed on the upper layer of other applications. This step is essential. Because it is currently based on Android 8.1.0, since Android 6.0, Google has converged on some sensitive permissions, such as accessing SD card permissions. It is not enough to declare permissions in the function list list. It also needs to dynamically check whether it is authorized during the application running. It is necessary to note that when it is checked that the application has not been granted these permissions, it is also necessary to remind users that some functions may not be used. This needs to be paid attention to.
private void checkMyPermission() { if (.SDK_INT >= Build.VERSION_CODES.M) { if (!(this)) { Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, ("package:" + getPackageName())); startActivityForResult(intent, 1); } } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 1) { if (.SDK_INT >= Build.VERSION_CODES.M) { if ((this)) { (this, "Authorization Success", Toast.LENGTH_SHORT).show(); }else { // SYSTEM_ALERT_WINDOW permission not granted... (this, "No permission was granted, related functions are not available", Toast.LENGTH_SHORT).show(); } } } }
Next, in the Service, do the following:
//Create global variable mHandler in Service private Handler mHandler; //Initialize mHandler in Service lifecycle method onCreate() mHandler = new Handler(()); //Add as follows where you want toast in the child thread (new Runnable() { @Override public void run() { //show dialog justShowDialog(); } }); private void justShowDialog() { builder = new (getApplicationContext()) .setIcon(.ic_dialog_info) .setTitle("Dialog pops up in service") .setMessage("Do you close the dialog?") .setPositiveButton("Sure", new () { public void onClick(DialogInterface dialog, int whichButton) { } }) .setNegativeButton("Cancel", new () { public void onClick(DialogInterface dialog, int whichButton) { } }); //The following line of code will be put into the child thread Can't create handler inside thread that has not called () AlertDialog dialog = (); //Settings Click elsewhere to cancel this Dialog (false); (false); //8.0 system strengthens background management and prohibits pop-ups of reminders in other applications and windows. If you want to bounce, you must use TYPE_APPLICATION_OVERLAY, otherwise it will not bounce. if (.SDK_INT >= Build.VERSION_CODES.O) { ().setType((.TYPE_APPLICATION_OVERLAY)); }else { ().setType((.TYPE_SYSTEM_ALERT)); } (); }
In this way, the Dialog can be popped up in "Android lower version -> Android 6.0 -> Android 8.0 -> higher Android version".
Summarize
The above is the method of popping up Dialog in Android 8.1.0 Service introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!
If you think this article is helpful to you, please reprint it. Please indicate the source, thank you!