问题
Anybody help me to make service type movable floating action button that displays on the screen of android device like button in Accessibility Scanner app https://play.google.com/store/apps/details?id=com.google.android.apps.accessibility.auditor&hl=en , i made button but want make it movable.
here is 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">
<com.shamanland.fab.FloatingActionButton
android:id="@+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:floatingActionButtonColor="#FFF"
app:floatingActionButtonSize="normal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.32"
android:src="@drawable/ic_accessibility_black_24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Here is my service
package com.example.accessibilitysoftware;
import android.accessibilityservice.AccessibilityService;
import android.graphics.PixelFormat;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.accessibility.AccessibilityEvent;
import android.widget.FrameLayout;
import android.widget.Toast;
import androidx.annotation.RequiresApi;
import com.shamanland.fab.FloatingActionButton;
public class Service extends AccessibilityService {
FrameLayout frameLayout;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP_MR1)
@Override
protected void onServiceConnected() {
// Create an overlay and display the action bar
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
frameLayout = new FrameLayout(this);
WindowManager.LayoutParams layoutParam = new WindowManager.LayoutParams();
layoutParam.type = WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY;
layoutParam.format = PixelFormat.TRANSLUCENT;
layoutParam.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
layoutParam.width = WindowManager.LayoutParams.WRAP_CONTENT;
layoutParam.height = WindowManager.LayoutParams.WRAP_CONTENT;
layoutParam.gravity = Gravity.TOP;
LayoutInflater inflater = LayoutInflater.from(this);
inflater.inflate(R.layout.service, frameLayout);
wm.addView(frameLayout, layoutParam);
configureFloatingActionButton();
}
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
}
@Override
public void onInterrupt() {
}
private void configureFloatingActionButton(){
final FloatingActionButton FLATButton = (FloatingActionButton) frameLayout.findViewById(R.id.floatingActionButton);
FLATButton.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(Service.this, "Clicked", Toast.LENGTH_SHORT).show();
return false;
}
});
}
}
Here is menifest
<application
...
<service
android:name=".Service"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/app_service" />
</service>
</application>
And here is res\xml\app_service.xml file
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityFeedbackType="feedbackGeneric"
android:accessibilityFlags="flagDefault"
android:accessibilityEventTypes="typeViewClicked|typeViewFocused"
android:canPerformGestures="true"
android:notificationTimeout="100"
android:canRetrieveWindowContent="true" />
来源:https://stackoverflow.com/questions/61423600/android-service-type-floating-action-button-with-drag-and-drop-functionality