老套路先看图
说下原理:
使用adb命令启动server然后传递参数,service拿到参数后可以根据需求实现模拟控制APP
看代码
package cn.yhsh.adbinputserver.service;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
import cn.yhsh.adbinputserver.utils.CommendUtils;
/**
* @author xiayiye5
* @date 2022/1/19 11:38
* adb输入传参使用方法如下 包名/启动的service全路径
* 播放 adb shell am start-foreground-service -n cn.yhsh.adbinputserver/cn.yhsh.adbinputserver.service.AdbService --ei position 0 --ei type 5
* 暂停 adb shell am start-foreground-service -n cn.yhsh.adbinputserver/cn.yhsh.adbinputserver.service.AdbService --ei position 1 --ei type 5
* 上一个 adb shell am start-foreground-service -n cn.yhsh.adbinputserver/cn.yhsh.adbinputserver.service.AdbService --ei position 2 --ei type 5
* 下一个 adb shell am start-foreground-service -n cn.yhsh.adbinputserver/cn.yhsh.adbinputserver.service.AdbService --ei position 3 --ei type 5
* 快进 adb shell am start-foreground-service -n cn.yhsh.adbinputserver/cn.yhsh.adbinputserver.service.AdbService --ei position 4 --ei type 5
* 快退 adb shell am start-foreground-service -n cn.yhsh.adbinputserver/cn.yhsh.adbinputserver.service.AdbService --ei position 5 --ei type 5
* 搜索 adb shell am start-foreground-service -n cn.yhsh.adbinputserver/cn.yhsh.adbinputserver.service.AdbService --ei position 6 --ei type 5
* 播放搜索 adb shell am start-foreground-service -n cn.yhsh.adbinputserver/cn.yhsh.adbinputserver.service.AdbService --ei position 7 --ei type 5
* 自动播放 adb shell am start-foreground-service -n cn.yhsh.adbinputserver/cn.yhsh.adbinputserver.service.AdbService --ei position 8 --ei type 5
* 上面传入的参数主要有position和type两个字段
*/
public class AdbService extends Service {
private static final String TAG = "AdbService";
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onCreate() {
super.onCreate();
try {
NotificationChannel notificationChannel = new NotificationChannel(
"cn.yhsh.adbinputserver",
"ChannelAsr", NotificationManager.IMPORTANCE_DEFAULT
);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.createNotificationChannel(notificationChannel);
startForeground(1, new NotificationCompat.Builder(this, "cn.yhsh.adbinputserver").build());
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int type = intent.getIntExtra("type", -1);
int position = intent.getIntExtra("position", -1);
Log.e(TAG, "打印position:" + position + "==" + type);
startForeground(1, new NotificationCompat.Builder(this, "cn.yhsh.adbinputserver").build());
return START_STICKY;
}
}
记得配置service后台运行权限
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
就可以了如果看不明白我提供一份源码: