Android 通过adb调用service原理
Android 通过重载service的dump函数实现adb调用函数
通过继承service实现dump函数,可以通过如adb shell dumpsys activity service com.flyme.mobileservice/.fcts.MyService + 自定义的命令(空格为分割符)
命令对service进行自定义命令和参数交互,来执行自己需要的代码函数。
public class MyService extends Service
public abstract class Service extends ContextWrapper implements ComponentCallbacks2 {
....
protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
throw new RuntimeException("Stub!");
}
}
所以只要在MyService中重载dump函数即可,对接收到的命令保存,然后解析做处理调用需要的目标函数,如:
@Override
public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
if (pw == null || args == null) {
Log.w(TAG, "writer or args wasn't null");
return;
} else {
mFd = fd;
mPw = pw;
}
for (int position = 0; position < args.length; position++) {
String opt = args[position];
Log.v(TAG, "position = [" + position + "]," + " cmd = [" + opt + "]");
if (position == 0) {
mType = opt;
}
switch (mType) {
case Type.SET:
mSetCommand[position] = opt;
break;
case Type.GET:
mGetCommand[position] = opt;
break;
case Type.EXE:
mExeCommand[position] = opt;
break;
case Type.HELP:
mHelpCommand[position] = opt;
break;
}
}
handleDump();
}
本文来自博客园,作者:小汀,转载请注明原文链接:https://www.cnblogs.com/1118zjg/p/17649097.html