gpt4 book ai didi

android - 检测到屏幕覆盖会阻止 Android 权限

转载 作者:IT王子 更新时间:2023-10-28 23:56:42 25 4
gpt4 key购买 nike

我注意到我的新手机上的 Android 应用出现了一个奇怪的问题。 SDK 23 权限弹出窗口(如外部存储)被下面附加的警报阻止。我最初认为这与我的手机有关,但它似乎不会影响我安装的任何其他应用程序。

这个问题可能与安装了调试版本有关,还是我的权限处理有问题?我认为它可能与我正在使用的广告平台之一有关,但我尝试禁用它们,但它仍然出现

enter image description here

我在下面粘贴了生成此权限请求的图像保存功能。我正在使用 Dexter为了节省编写一大堆可怕的样板文件

public static void saveToExternalStorageIfAllowed(final Context context, final Bitmap bitmapImage, final String title) {
final Tracker t = ((LoLHistory) context.getApplicationContext()).getTracker(LoLHistory.TrackerName.APP_TRACKER);

// saving to publicly visible/accessible folder. Requires write permission
int permissionCheck = ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
// do not have permissions to write, request
t.send(new HitBuilders.EventBuilder()
.setCategory("FILE")
.setAction("PermissionMissing")
.setLabel("WRITE_EXTERNAL")
.build());
Dexter.checkPermission(new PermissionListener() {
@Override
public void onPermissionGranted(PermissionGrantedResponse response) {
t.send(new HitBuilders.EventBuilder()
.setCategory("FILE")
.setAction("PermissionGranted")
.setLabel("WRITE_EXTERNAL")
.build());

saveToExternalStorage(context, bitmapImage, title);
}

@Override
public void onPermissionDenied(PermissionDeniedResponse response) {
t.send(new HitBuilders.EventBuilder()
.setCategory("FILE")
.setAction("PermissionDenied")
.setLabel("WRITE_EXTERNAL")
.build());
}

@Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {/* ... */}
}, Manifest.permission.WRITE_EXTERNAL_STORAGE);
} else {
saveToExternalStorage(context, bitmapImage, title);
}
}

private static void saveToExternalStorage(Context context, Bitmap bitmapImage, String title) {
Tracker t = ((LoLHistory) context.getApplicationContext()).getTracker(LoLHistory.TrackerName.APP_TRACKER);

// create image folder if does not exist
File imagesFolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), context.getString(R.string.app_name));
if (!imagesFolder.mkdirs() && !imagesFolder.isDirectory()) {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// failed to create and is not a directory. Something went wrong...
t.send(new HitBuilders.EventBuilder()
.setCategory("FILE")
.setAction("CreateDirFailed")
.setLabel(imagesFolder.getPath())
.build());
} else {
t.send(new HitBuilders.EventBuilder()
.setCategory("FILE")
.setAction("CreateDirFailedMediaNotMounted")
.setLabel(imagesFolder.getPath())
.build());
}
}

// delete image if already exists so FOS can create a new one
File image = new File(imagesFolder, title + ".jpg");
if (image.exists()) {
// image already exists, deleting to start from clean state
if (!image.delete()) {
// failed to delete
t.send(new HitBuilders.EventBuilder()
.setCategory("FILE")
.setAction("DeleteFailed")
.setLabel(image.getPath())
.build());
}
}

// compress bitmap and write to file stream. FOS creates file if does not exist
FileOutputStream out = null;
try {
out = new FileOutputStream(image);
bitmapImage.compress(Bitmap.CompressFormat.JPEG, 50, out);
out.flush();
} catch (Exception e) {
e.printStackTrace();
t.send(new HitBuilders.ExceptionBuilder()
.setDescription(e.getLocalizedMessage())
.setFatal(true)
.build());
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
t.send(new HitBuilders.ExceptionBuilder()
.setDescription(e.getLocalizedMessage())
.setFatal(true)
.build());
}
}

// get Uri from saved image
Uri uriSavedImage = Uri.fromFile(image);

// media scan the new file so it shows up in the gallery
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(uriSavedImage);
context.sendBroadcast(mediaScanIntent);
}

更新:由于很多人都提到它,如前所述,此问题不是由于安装了覆盖应用程序。在 Draw over other apps 菜单下,我有以下应用程序:Google Play 音乐、Google Play 服务、照片、TalkBack、Twitch、Twitter。 所有这些都设置为否

此外,我还测试了其他应用程序,例如 Google Hangouts 和 Twitter,它们也具有需要危险权限的操作,我能够提供这些权限而不会出现此问题。


解决方案:我已标记R. Zagorski的答案作为解决方案,因为它包括很多一般情况。对我来说,实际上是一个 Toast 打破了我的权限流程。这个弹出窗口浪费了我很多时间,让我走错了路……

这是我在权限弹出窗口出现后的最初几秒钟内看到的 Toast:

enter image description here

最佳答案

此弹出窗口是由 manifest.PERMISSION.SYSTEM_ALERT_WINDOW 引起的 list 声明的权限。开发人员必须注意 3 类权限。

  1. 正常许可 - 对它们不做任何事情,只需在 list 中声明
  2. Vulnerable permissions - 在 Manifest 中声明并在第一时间请求许可。它们可以通过系统设置进行更改
  3. 危险权限以上:SYSTEM_ALERT_WINDOWWRITE_SETTINGS属于这一类。它们必须被授予,但在系统设置中不可见。要请求它,您不使用标准方式 (int checkSelfPermission (String permission)),但您必须检查 Settings.canDrawOverlays()Settings。 System.canWrite() 适当

如果您没有 SYSTEM_ALERT_WINDOW权限:

  1. 在与权限弹出窗口交互时检查您是否有一个可见的 Toast 虽然 Overlay Detected 弹出窗口没有提及它,但一个 Toast 也算作叠加层
  2. 检查是否有任何依赖项需要它

如果您不确定自己是否使用此权限,可以执行以下几个测试用例:

  1. 自行申请此权限:

    public class MainActivity extends AppCompatActivity {

    public final static int REQUEST_CODE = 10101;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (checkDrawOverlayPermission()) {
    startService(new Intent(this, PowerButtonService.class));
    }
    }

    public boolean checkDrawOverlayPermission() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
    return true;
    }
    if (!Settings.canDrawOverlays(this)) {
    Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
    Uri.parse("package:" + getPackageName()));
    startActivityForResult(intent, REQUEST_CODE);
    return false;
    } else {
    return true;
    }
    }

    @Override
    @TargetApi(Build.VERSION_CODES.M)
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE) {
    if (Settings.canDrawOverlays(this)) {
    startService(new Intent(this, PowerButtonService.class));
    }
    }
    }
    }
  2. 检查 thisthis不是你的情况

  3. 查看 this post请注意,通过 Play 商店安装应用程序时,会自动授予此权限(我没有检查,因此无法确认)

权限模型可以理解,只是有时候需要多挖掘。

关于android - 检测到屏幕覆盖会阻止 Android 权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39174846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com