参考:https://blog.csdn.net/u013356254/article/details/52287794
实现基本跟链接相同,不同只出在于WindowManager.LayoutParams的type设为了WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, 按照参考链接设置TYPE_SYSTEM_ERROR运行时会报错,
android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@398b86a -- permission denied for window type 2010
源码如下:
1.MainActivity
package com.example.zujian;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Bundle;
import android.os.Looper;
import android.provider.Settings;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
permission();
}
@Override
protected void onResume() {
super.onResume();
}
public void onClick(View view) {
new Thread(new Runnable() {
WindowManager wm;
TextView tv;
int width;
int height;
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void run() {
Looper.prepare();
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
final View view1 = View.inflate(MainActivity.this, R.layout.item, null);
tv = view1.findViewById(R.id.tv);
final WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
params.width = (int)(60 * getResources().getDisplayMetrics().density);
params.height = (int)(60 * getResources().getDisplayMetrics().density);
params.gravity = Gravity.START | Gravity.TOP;
params.format = PixelFormat.TRANSPARENT;
width = wm.getDefaultDisplay().getWidth();
height = wm.getDefaultDisplay().getHeight();
params.y = height / 2 - params.height / 2;
wm.addView(view1, params);
view1.setOnTouchListener(new View.OnTouchListener() {
private int x, y;
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
x = (int) motionEvent.getRawX();
y = (int) motionEvent.getRawY();
break;
case MotionEvent.ACTION_MOVE:
int minX = (int) (motionEvent.getRawX() - x);
int minY = (int) (motionEvent.getRawY() - y);
params.x = Math.min(width - params.width, Math.max(0, minX + params.x));
params.y = Math.min(height - params.height, Math.max(0, minY + params.y));
wm.updateViewLayout(view1, params);
x = (int) motionEvent.getRawX();
y = (int) motionEvent.getRawY();
break;
case MotionEvent.ACTION_UP:
if(params.x > 0 && params.x < width - params.width) {
int x = params.x;
if(x > (width - params.width) / 2) {
params.x = width - params.width;
}else {
params.x = 0;
}
wm.updateViewLayout(view1, params);
}else if(params.x == 0 || params.x == width - params.width) {
Toast.makeText(MainActivity.this, "被电击了", Toast.LENGTH_SHORT).show();
tv.setText("哇哇");
}
break;
}
return true;
}
});
Looper.loop();
}
}).start();
}
public void permission(){
if (Build.VERSION.SDK_INT >= 23) {
if(!Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
startActivity(intent);
}
}
}
}
2.布局
-
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:onClick="onClick" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
-
item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_blue_bright"> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/app_name" /> </LinearLayout>
3.AndroidManifest.xml
声明了使用SYSTEM_ALERT_WINDOW权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.zujian">
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>