1.点击事件启动服务
public void startFloatingButtonService(View view) {
Log.e("TAG", "startFloatingButtonService: "+FloatingButtonService.isStarted);
if (FloatingButtonService.isStarted) {
return;
}
if (!Settings.canDrawOverlays(this)) {
Toast.makeText(this, "当前无权限,请授权", Toast.LENGTH_SHORT);
startActivityForResult(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName())), 0);
} else {
startService(new Intent(MainActivity.this, FloatingButtonService.class));
}
}
2.服务代码
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import com.example.xuanfuactivity.R;
import android.app.Service;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.Nullable;
/**
* Created by dongzhong on 2018/5/30.
*/
public class FloatingButtonService extends Service {
public static boolean isStarted = false;
private WindowManager windowManager;
private WindowManager.LayoutParams layoutParams;
private View mDesktopLayout;
private Button button,b2;
private ImageView imageView;
private int imgs [] ={
R.mipmap.img1,
R.mipmap.img2,
R.mipmap.img3,
R.mipmap.img4,
R.mipmap.img5,
R.mipmap.img6,
R.mipmap.gif
};
private int index=0;
@Override
public void onCreate() {
super.onCreate();
isStarted = true;
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
layoutParams = new WindowManager.LayoutParams();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
}
layoutParams.format = PixelFormat.RGBA_8888;
layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
layoutParams.width = 500;
layoutParams.height = 800;
layoutParams.x = 0;
layoutParams.y = 0;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
showFloatingWindow();
return super.onStartCommand(intent, flags, startId);
}
private void showFloatingWindow() {
if (Settings.canDrawOverlays(this)) {
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mDesktopLayout = inflater.inflate(R.layout.xuanfu_view_diy, null);
imageView=mDesktopLayout.findViewById(R.id.i1);
imageView.setImageResource(imgs[0]);
button=mDesktopLayout.findViewById(R.id.b1);
button.setOnClickListener(new onClicl());
b2=mDesktopLayout.findViewById(R.id.b2);
b2.setOnClickListener(new onClicl());
mDesktopLayout.setOnTouchListener(new FloatingOnTouchListener());
windowManager.addView(mDesktopLayout, layoutParams);
}
}
public class onClicl implements View.OnClickListener {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.b1:
if(index<6){
index=index+1;
}else{
index=0;
}
imageView.setImageResource(imgs[index]);
break;
case R.id.b2:
Log.e("", "onClick: 关闭" );
windowManager.removeView(mDesktopLayout);
isStarted=false;
break;
}
}
}
private class FloatingOnTouchListener implements View.OnTouchListener, View.OnClickListener, View.OnLongClickListener {
private int x;
private int y;
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x = (int) event.getRawX();
y = (int) event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
int nowX = (int) event.getRawX();
int nowY = (int) event.getRawY();
int movedX = nowX - x;
int movedY = nowY - y;
x = nowX;
y = nowY;
layoutParams.x = layoutParams.x + movedX;
layoutParams.y = layoutParams.y + movedY;
windowManager.updateViewLayout(view, layoutParams);
break;
default:
break;
}
return false;
}
@Override
public void onClick(View v) {
Log.e("TAG", "onClick: " );
}
@Override
public boolean onLongClick(View v) {
return false;
}
}
}
3.服务配置代码
<service
android:name=".service.FloatingButtonService"
android:enabled="true"
android:exported="true" />
4.悬浮框界面(xuanfu_view_diy)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#000099"
android:layout_gravity="center"
>
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="@+id/i1"
></ImageView>
<Button
android:id="@+id/b1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="切换下一页"
android:textColor="#ffffff"></Button>
<Button
android:id="@+id/b2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="关闭悬浮框"
android:textColor="#ffffff"></Button>
</LinearLayout>