您好,您可以帮我创建辅助浮动按钮吗? as this image
项目在这里:https://github.com/Mirrorhanyu/AssistiveTouch,但是它很旧,如果我尝试不起作用
我没有输入任何代码,因为我想重新开始 清单
<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>
<service
android:name=".FloatingWidgetService"
android:enabled="true"
android:exported="false" />
</application>
我的主要活动是我允许使用android参数中的权限
public class MainActivity extends AppCompatActivity {
private static final int DRAW_OVER_OTHER_APP_PERMISSION = 123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
askForSystemOverlayPermission();
Button button = findViewById(R.id.start);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,FloatingWidgetService.class);
startService(intent);
}
});
}
private void askForSystemOverlayPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, DRAW_OVER_OTHER_APP_PERMISSION);
}
}
我的floatwidgetservice,一开始我将其添加到gradle的“ implementation'com.github.andremion:counterfab:1.0.1'”中,用于counterfab
public class FloatingWidgetService extends Service {
private WindowManager mWindowManager;
private View mOverlayView;
CounterFab counterFab;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
setTheme(R.style.AppTheme);
mOverlayView = LayoutInflater.from(this).inflate(R.layout.overlay_layout, null);
final 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; //Initially view will be added to top-left corner
params.x = 0;
params.y = 100;
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mWindowManager.addView(mOverlayView, params);
counterFab = (CounterFab) mOverlayView.findViewById(R.id.fabHead);
counterFab.setCount(1);
counterFab.setOnTouchListener(new View.OnTouchListener() {
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
initialX = params.x;
initialY = params.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
return true;
case MotionEvent.ACTION_UP:
return true;
case MotionEvent.ACTION_MOVE:
float Xdiff = Math.round(event.getRawX() - initialTouchX);
float Ydiff = Math.round(event.getRawY() - initialTouchY);
params.x = initialX + (int) Xdiff;
params.y = initialY + (int) Ydiff;
mWindowManager.updateViewLayout(mOverlayView, params);
return true;
}
return false;
}