Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何让所有应用程序上的浮动触摸_Android_Overlay - Fatal编程技术网

Android 如何让所有应用程序上的浮动触摸

Android 如何让所有应用程序上的浮动触摸,android,overlay,Android,Overlay,我正在尝试创建一个可点击的图像,它位于所有应用程序和甲板顶部,就像facebook messenger一样 这是我的密码 public class FloatService extends Service { private WindowManager windowManager; private ImageView imageBtn; @Override public IBinder onBind(Intent intent) { re

我正在尝试创建一个可点击的图像,它位于所有应用程序和甲板顶部,就像facebook messenger一样

这是我的密码

public class FloatService extends Service
{
    private WindowManager windowManager;
    private ImageView imageBtn;

    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }

    @Override
    public void onCreate()
    {
        super.onCreate();

        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        imageBtn = new ImageView(this);
        imageBtn.setImageResource(R.drawable.ic_launcher);

        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

        params.gravity = Gravity.TOP | Gravity.LEFT;
        params.x = 0;
        params.y = 100;

        windowManager.addView(imageBtn, params);
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        if (imageBtn != null)
            windowManager.removeView(imageBtn);
    }
}


我的活动中有两个按钮,一个是“开始服务”,另一个是“停止服务” 我点击“启动服务”,它会创建一个可点击的图像,所以一切正常。 但当我切换到其他应用程序或离开此应用程序时,可单击的图像将消失,直到我打开我的应用程序


感谢您的帮助

发生这种情况是因为当您退出应用程序时,由于内存不足,您的服务已被破坏

所以 1.您可以通过Service.onStartCommand()传递START\u

2.您可以作为前台服务启动服务

安卓操作系统并没有真正扼杀前台服务


这可能会帮助您将窗口类型从
WindowManager.LayoutParams.type\u PHONE
更改为
WindowManager.LayoutParams.type\u SYSTEM\u ALERT

WindowManager.LayoutParams params = new WindowManager.LayoutParams(
    WindowManager.LayoutParams.
    WindowManager.LayoutParams.WRAP_CONTENT,
    WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, // modified
    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
    PixelFormat.TRANSLUCENT);
我找到了解决办法

因为我的手机是MIUI,它默认隐藏任何浮动窗口

所以我需要自己打开它

谢谢你的回答

WindowManager.LayoutParams params = new WindowManager.LayoutParams(
    WindowManager.LayoutParams.
    WindowManager.LayoutParams.WRAP_CONTENT,
    WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, // modified
    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
    PixelFormat.TRANSLUCENT);