In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to pop up Dialog in Android8.1.0Service", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn how to pop up Dialog in Android8.1.0Service "this article.
Scenario: open the thread to download the upgrade package in Service, and when the system upgrade package is downloaded, a Dialog pops up to prompt the user.
Note that different versions of the Android system may behave differently. Currently, Service based on Android 8.1.0 has been hit by Dialog.
First, to declare permissions in the function list list, both of the following must be declared:
It is necessary to check again whether the current application is allowed to be displayed at the top of other applications while MainActivity is initializing. Because it is currently based on Android 8.1.0, Google has converged some sensitive permissions since Android 6.0.For example, it is not enough to access the SD card, only to declare permissions in the function list list, and to dynamically check whether the application is authorized while the application is running. note that when you check that the application is not granted these permissions, you should also remind users that some functions may not be available.
Private void checkMyPermission () {if (Build.VERSION.SDK_INT > = Build.VERSION_CODES.M) {if (! Settings.canDrawOverlays (this)) {Intent intent = new Intent (Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse ("package:" + getPackageName (); startActivityForResult (intent, 1) } @ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {if (requestCode = = 1) {if (Build.VERSION.SDK_INT > = Build.VERSION_CODES.M) {if (Settings.canDrawOverlays (this)) {Toast.makeText (this, "authorization successful", Toast.LENGTH_SHORT). Show ();} else {/ / SYSTEM_ALERT_WINDOW permission not granted... Toast.makeText (this, "No permission granted, related features not available", Toast.LENGTH_SHORT) .show ();}
Next, in Service, do the following:
/ / create a global variable mHandler private Handler mHandler;// in Service initialize mHandler mHandler = new Handler (Looper.getMainLooper ()) in the Service lifecycle method onCreate (); / / add the following mHandler.post (new Runnable () {@ Override public void run () {/ / show dialog justShowDialog ();}}) where you want Toast in the child thread) Private void justShowDialog () {AlertDialog.Builder builder = new AlertDialog.Builder (getApplicationContext ()) .setIcon (android.R.drawable.ic_dialog_info) .setTitle ("Dialog popped up in service") .setMessage ("do you want to close dialog?") .setPositiveButton ("OK", new DialogInterface.OnClickListener () {public void onClick (DialogInterface dialog, int whichButton) {}) .setNegativeButton ("cancel", new DialogInterface.OnClickListener () {public void onClick (DialogInterface dialog, int whichButton) {}); / / the following line of code will Can't create handler inside thread that has not called Looper.prepare () AlertDialog dialog = builder.create () / / Click elsewhere can not cancel this Dialog dialog.setCancelable (false); dialog.setCanceledOnTouchOutside (false); / / 8.0 system strengthen background management, prohibit pop-up window in other applications and windows, if you want to play, you must use TYPE_APPLICATION_OVERLAY, otherwise you will not be able to pop up if (Build.VERSION.SDK_INT > = Build.VERSION_CODES.O) {dialog.getWindow () .setType ((WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY)) } else {dialog.getWindow () .setType ((WindowManager.LayoutParams.TYPE_SYSTEM_ALERT));} dialog.show ();}
In this way, Dialog can be popped up in "Android low version-> Android 6.0-> Android 8.0-> higher Android version".
The above is all the contents of the article "how to pop up Dialog in Android8.1.0Service". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.