0

我正在构建一个简单的 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 命令?

4

1 回答 1

1

您应该使用方法 -[setArguments:] 设置参数,例如这个示例NSTask shell

于 2015-01-28T01:57:03.353 回答