简体   繁体   中英

Runtime permission for SYSTEM_ALERT_WINDOW

I need to get the user permissions automatically for the following permissions while user opens the app for the first time

  1. Phone
  2. Call logs
  3. Always on top

Phone is working fine. How to get the always on top permission

MainActivity

protected void onCreate(Bundle savedInstanceState) {
    if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.SYSTEM_ALERT_WINDOW)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.SYSTEM_ALERT_WINDOW}, 1);
    }
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

Manifest

<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

there is no runtime permission/dialog for this purpose, you have to pass user to app settings

public boolean checkStartPermissionRequest() {
    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, ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE);
            return false; // above will start new Activity with proper app setting
        }
    }
    return true; // on lower OS versions granted during apk installation
}

more info HERE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM