Android通过adb命令传参给APP的方法

老套路先看图

 

 说下原理:

使用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" />

就可以了如果看不明白我提供一份源码:

adb传参APP源码下载

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值