Andrews auxiliary keys (similar QuickMacro)

 

Accessibility and screenshot function root of the phone will not say

 

Accessibility:

package com.yangdc.auto.games.stzb;


import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.GestureDescription;
import android.accessibilityservice.GestureDescription.StrokeDescription;
import android.graphics.Path;
import android.os.Build;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;

import androidx.annotation.RequiresApi;

import com.blankj.utilcode.util.ToastUtils;


public class MyAccessibilityService extends AccessibilityService {

    final String TAG = "Ydc";

    public static MyAccessibilityService mService;
    public static boolean isStart() {
        return  mService != null;
    }

    @Override
    public void onInterrupt() {
    }
    @Override
    public void onServiceConnected() {
        mService = this;
        //初始化
        super.onServiceConnected();
        ToastUtils.showShort("无障碍已开启");
    }
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        mService = this;
    }
    public Boolean keyevent(int action)  {
        Log.d(TAG, "按下:" + action);
        return this.performGlobalAction(action);
    }


    @RequiresApi(api = Build.VERSION_CODES.N)
    public void dispatchGestureClick(int x, int y){
        Path path=new Path();
        path.moveTo(x, y);

        GestureDescription.Builder builder = new GestureDescription.Builder();
        builder.addStroke(new StrokeDescription(path, 10, 100));
        GestureDescription gestureDescription = builder.build();
        dispatchGesture(gestureDescription,null,null);
        Log.d(TAG,"点击坐标 " + x + " " + y);

    }

    public void swipe(int x1,int y1,int x2,int y2,int time){
        Path path=new Path();
        path.moveTo(x1, y1);
        path.lineTo(x2,y2);
        GestureDescription.Builder builder = new GestureDescription.Builder();
        builder.addStroke(new StrokeDescription(path, 10, time));
        GestureDescription gestureDescription = builder.build();
        dispatchGesture(gestureDescription,null,null);
        Log.d(TAG,"滑动 " + x1 + " " + y1 + " " + x2 + "  "+ y2);
    }

}

  

Screenshot:

package com.yangdc.auto.games.stzb;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.display.DisplayManager;
import android.hardware.display.VirtualDisplay;
import android.media.Image;
import android.media.ImageReader;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Build;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.DisplayMetrics;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.blankj.utilcode.util.ImageUtils;

import java.nio.ByteBuffer;

import static android.media.ImageReader.*;

public class ImageReaderService extends Service {
    public static ImageReaderService service;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    int width;
    int height;
    int dpi;
    ImageReader imageReader;
    MediaProjection mediaProjection;
    MediaProjectionManager projectionManager;
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        service = this;
        Log.d(TAG, "onStartCommand()");
        setForeground();
        DisplayMetrics metric = new DisplayMetrics();

        MainActivity.Main.getWindowManager().getDefaultDisplay().getMetrics(metric);
        width = metric.widthPixels;
        height = metric.heightPixels + MainActivity.Main.getStatusBarHeight();
        dpi = metric.densityDpi;

        int     resultCode = intent.getIntExtra("code", -1);
        Intent   data = intent.getParcelableExtra("data");
        projectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);

        mediaProjection = projectionManager.getMediaProjection(resultCode,data);
        return super.onStartCommand(intent,flags,startId);
    }
    private static final String TAG="MyService";
    private static final String ID="channel_1";
    private static final String NAME="前台服务";
    boolean isOk = false;
    public void setForeground(){
        if (isOk){
            return;
        }
        isOk = true;
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) {
            return;
        }
        NotificationManager manager=(NotificationManager)getSystemService (NOTIFICATION_SERVICE);
        NotificationChannel channel= null;
        channel = new NotificationChannel(ID,NAME, NotificationManager.IMPORTANCE_HIGH);

        assert manager != null;
        manager.createNotificationChannel (Channel); 
        the Notification the Notification = null; 
        the Notification = new new Notification.Builder (the this, ID) 
                .setContentTitle ( "receive an important notice") 
                .setContentText ( "This is an important notice") 
                .setSmallIcon (R.mipmap .ic_launcher) 
                .setLargeIcon (BitmapFactory.decodeResource (getResources (), R.mipmap.ic_launcher)) 
                .build (); 

        startForeground (. 1, Notification); 
    } 

    public static Bitmap ScreenShot () { 

        if (service != null){
            return service.startCapture (); 
        } 
        the Log .e (TAG, "did not start the service"); 
        return null; 
    }
    private synchronized Bitmap startCapture() {
        if (imageReader != null){
            return null;
        }
        VirtualDisplay virtualDisplay = null;
        Bitmap  bitmap = null;
        imageReader = newInstance(width, height, PixelFormat.RGBA_8888, 1);
        Log.d(TAG,"dpi" + dpi);
        virtualDisplay = mediaProjection.createVirtualDisplay("ScreenShout",
                width,height,dpi,
                DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
                imageReader.getSurface(),null,null);
        try {


            Image image = null;
            int times = 0;
            do {
                times++;
                SystemClock.sleep(100);
                image = imageReader.acquireNextImage();
                if (image != null){
                    break;
                }
            }while (times < 2000);

            if (image != null){

            Log.d(TAG,"新图片");
            int width = image.getWidth();
            int height = image.getHeight();
            final Image.Plane[] planes = image.getPlanes();
            final ByteBuffer buffer = planes[0].getBuffer();
            int pixelStride = planes[0].getPixelStride();
            int rowStride = planes[0].getRowStride();
            int rowPadding = rowStride - pixelStride * width;
            bitmap = Bitmap.createBitmap(width + rowPadding / pixelStride, height, Bitmap.Config.ARGB_8888);
            bitmap.copyPixelsFromBuffer(buffer);
           Bitmap  bitmap1 = Bitmap.createBitmap(bitmap, 0, 0,width, height);
           bitmap = bitmap1;
            Log.d(TAG,bitmap.getByteCount()+"bitmap.getByteCount()" + "rowPadding / pixelStride" + (rowPadding / pixelStride));


            }else{
                Log.d(TAG,"图片 为空");
            }

            if (image != null) {
                image.close();
            }

        }catch (Exception ignored){
        }finally {
            if (imageReader != null) {
                imageReader.discardFreeBuffers();
            }
            imageReader.close();
            imageReader = null;
            if (virtualDisplay != null){
                virtualDisplay.release();
                virtualDisplay = null;
            }
        }
        return bitmap;
    }
}

  

 

Then C # adb is considered the root so it is nothing to say

 

You can see the complete code 

https://gitee.com/yangdc/AutoAndroid

High version of the pit or not finishing a lot of code cloud project is to develop the learning process

The project achieved a lot for personal use but not very practical function ..... popular use requires some understanding of the job is not a fool

 

Guess you like

Origin www.cnblogs.com/yangdc/p/12669508.html