使用NSTask运行adb命令

问题描述 投票:0回答:1

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

objective-c cocoa adb nstask nspipe
1个回答
1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.