在应用外请求录制屏幕权限需要在应用中添加以下代码。
static final int REQUEST_CODE = 123;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
requestRecordingPermission();
}
public void requestRecordingPermission() { 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, REQUEST_CODE); } else { //已经授权 } } else { //不需要授权 } }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_CODE) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Settings.canDrawOverlays(this)) { //已经授权 } else { //未授权 } } } }
这样,在应用外请求录制屏幕权限时会弹出系统级别的权限请求弹窗,用户点击允许则可以在应用外录制屏幕。