日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

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

發布時間:2023/12/15 Android 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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@Overridepublic IBinder onBind(Intent intent) {return null;}@RequiresApi(api = Build.VERSION_CODES.O)@Overridepublic 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());}}@Overridepublic 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源碼下載

總結

以上是生活随笔為你收集整理的Android通过adb命令传参给APP的方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。