Explain the method of ejecting Dialog in Android 8.1. 0 Service in detail

  • 2021-11-13 02:52:27
  • OfStack

Scenario: Open the thread in Service to download the upgrade package. When the system upgrade package is downloaded, an Dialog will pop up to prompt the user.

Note that Android system versions are different, and may have different performances. At present, Service is shot Dialog based on Android 8.1. 0.

First of all, you want to declare permissions in the feature list, both of which must be declared:


<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/><!-- This line of code must exist, otherwise the button in the system settings cannot be clicked -->
  <uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />

Then, when MainActivity is initialized, check again whether the current application under 1 is allowed to be displayed on the upper layer of other applications. This step is essential. Because 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 list of functions. It is necessary to dynamically check whether the application is authorized during running. It should be noted that when it is checked that the application has not been granted these permissions, it should also remind users that there may be some functions that cannot be used, which needs attention.


 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 succeeded ", Toast.LENGTH_SHORT).show();
        }else {
          // SYSTEM_ALERT_WINDOW permission not granted...
          Toast.makeText(this, " No permissions have been granted, and related functions are unavailable ", Toast.LENGTH_SHORT).show();
        }
      }
    }
  }

Next, in Service, do the following:


// In  Service  Create global variables in  mHandler
  private Handler mHandler;
// In  Service  Life cycle method  onCreate()  Initialization in  mHandler
  mHandler = new Handler(Looper.getMainLooper());
// In a child thread, you want  Toast  Add the following places 
  mHandler.post(new Runnable() {
          @Override
          public void run() {
            //show dialog
            justShowDialog();
          }
        });
 
  private void justShowDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext())
        .setIcon(android.R.drawable.ic_dialog_info)
        .setTitle("service Middle ejection Dialog It's over ")
        .setMessage(" Whether to close dialog ? ")
        .setPositiveButton(" Determine ",
            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 is placed in a child thread  Can't create handler inside thread that has not called Looper.prepare()
    AlertDialog dialog = builder.create();
    // You can't cancel this setting by clicking elsewhere  Dialog
    dialog.setCancelable(false);
    dialog.setCanceledOnTouchOutside(false);
    //8.0 The system strengthens the background management, and it is forbidden to play reminder pop-ups in other applications and windows. If you want to play, you must use TYPE_APPLICATION_OVERLAY Or it won't 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, in the lower version of Android- > Android 6.0 - > Android 8.0 - > Higher Android version "can pop up Dialog.

Summarize


Related articles: