Android 如何在其他应用中叠加视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20461979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:54:45  来源:igfitidea点击:

How to overlay views in other apps

android

提问by NaviRamyle

Where do I start?

我从哪里开始?

I don't know what functions or permissions will I use to make this. No root required.

我不知道我将使用什么功能或权限来实现这一点。无需root。

The view look like this, the camera button on the right side, it is floating and visible to other apps, if you push it, it will capture a screenshot.

视图看起来像这样,右侧的相机按钮,它是浮动的,其他应用程序可见,如果您按下它,它将捕获屏幕截图。

enter image description here

在此处输入图片说明

Note: I will not make make a screenshot app, this is only an example of what I want to achieve.

注意:我不会制作截图应用程序,这只是我想要实现的一个例子。

回答by David LM

Try this:

尝试这个:

    if(!isSystemAlertPermissionGranted(MainActivity.this)){
        requestSystemAlertPermission(MainActivity.this,1);
    }

    startService(new Intent(getApplicationContext(), Overlay.class));

And:

和:

public static void requestSystemAlertPermission(Activity context, int requestCode) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
        return;
    final String packageName = context == null ? context.getPackageName() : context.getPackageName();
    final Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + packageName));
    if (context != null)
        context.startActivityForResult(intent, requestCode);
    else
        context.startActivityForResult(intent, requestCode);
}
@TargetApi(23)
public static boolean isSystemAlertPermissionGranted(Context context) {
    final boolean result = Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP || Settings.canDrawOverlays(context);
    return result;
}