我正在尝试获得在棉花糖中绘制叠加层的许可。
在棉花糖中,我们必须为绘制叠加层提供运行时许可,我已经实施了。我得到的问题是,如果我检查下面的代码,下次我从我的应用程序获得了许可,如果(Settings.canDrawOverlays(getApplicationContext())它返回false。
但在设置中(绘制其他应用程序)我的应用程序已检查。在这里,我提供了 lib 活动的代码。
if(Settings.canDrawOverlays(getApplicationContext())&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
Toast.makeText(getApplicationContext(), "Windows Overlay allowed" , Toast.LENGTH_SHORT).show();
startService(new Intent(this, FABService.class));
finish();
}
else{
startActivityForResult(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION), 1);
}
和我的 onActivityResult 是,
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
/** check if received result code
is equal our requested code for draw permission */
if (requestCode == 1) {
// * if so check once again if we have permission /
if (Settings.canDrawOverlays(getApplicationContext()) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// continue here - permission was granted
Toast.makeText(getApplicationContext(), "Windows Overlay allowed", Toast.LENGTH_LONG).show();
startService(new Intent(this, FABService.class));
finish();
}
}
}
我在该应用程序的lib中实现了这些代码。任何1都可以帮助我在哪里出错。
答案 0 :(得分:2)
在Manifest中放置权限
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
将此用于覆盖权限
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
// Show alert dialog to the user saying a separate permission is needed
// Launch the settings activity if the user prefers
Intent myIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
startActivity(myIntent);
}