【问题标题】:Run adb commands with NSTask使用 NSTask 运行 adb 命令
【发布时间】:2015-03-26 19:03:26
【问题描述】:

我正在构建一个简单的 GUI 来在 Cocoa 中运行 ADB 命令。我发现了几篇不同的文章,介绍了如何使用 NSTask 运行 shell 命令,但没有专门针对 ADB,而且我无法理解。

我可以运行简单的 ADB 命令,例如
- adb devices
- adb reboot

功能

NSString *adbPath = @"~/Android/sdk/platform-tools/adb";
NSString* runADBCommand(NSString *cmd)
{
    NSTask *adbDevices = [[NSTask alloc] init];
    [adbDevices setLaunchPath:adbPath];

    adbDevices.arguments = @[cmd];

    NSPipe *pipe;
    pipe = [NSPipe pipe];
    [adbDevices setStandardOutput:pipe];

    NSFileHandle *file;
    file = [pipe fileHandleForReading];

    [adbDevices launch];

    NSData *data;
    data = [file readDataToEndOfFile];

    NSString *adbComOutput;
    adbComOutput = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    return adbComOutput;
}

致电

- (void)refreshDevices:(id)sender
{
    adbOutput.stringValue = runADBCommand(@"devices");
}

上面的工作正常,但是当我传递一个更复杂的参数时:

- (void) getVersion:(id)sender
{
    runADBCommand(@"shell cat /system/build.prop | grep incremental");
}

我只是得到控制台输出,就好像我刚刚在终端中输入了adb。如何打包 NSTask 的命令和参数来运行 ADB 命令?

【问题讨论】:

  • adbPath 是如何初始化的?
  • 我把它作为一个全局变量NSString *adbPath = @"~/Android/sdk/platform-tools/adb";

标签: objective-c cocoa adb nstask nspipe


【解决方案1】:

你应该使用方法 -[setArguments:] 设置参数,就像这个例子NSTask shell

【讨论】:

  • 正是我想要的!非常感谢!
猜你喜欢
  • 2011-10-17
  • 2012-03-13
  • 1970-01-01
  • 2012-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-01
相关资源
最近更新 更多