13

I am trying to execute adb commands programmatically

Here is my code:

File f = new File(Environment.getExternalStorageDirectory(), "screen" + System.currentTimeMillis() + ".png");

new ExecuteCommand(MainActivity.this).execute("adb shell screencap -p "
        + Environment.getExternalStorageDirectory().getPath() +
        "/" + "screen" + System.currentTimeMillis() + ".png");

ExecuteCommand Class:

public class ExecuteCommand extends AsyncTask<String, String, String> {

    Context mContext=null;
    public ExecuteCommand(Context _ctx)
    {
        mContext =_ctx;
    }

    ProgressDialog progressdailog;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressdailog = ProgressDialog.show(mContext,
                "Executing", "Please Wait");
    }
    @Override
    protected String doInBackground(String... params) {
        Process p;
        StringBuffer output = new StringBuffer();
        try {
            p = Runtime.getRuntime().exec(params[0]);
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(p.getInputStream()));
            String line = "";
            while ((line = reader.readLine()) != null) {
                output.append(line + "\n");
                p.waitFor();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String response = output.toString();
        return response;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        progressdailog.dismiss();
        Log.e("Output", result);
    }
}

Problem in logs:

07-31 15:26:31.832 18716-18716/com.example E/Output﹕ cannot bind 'tcp:5038' * daemon not running. starting it now on port 5038 *

I am not able to take a screenshot, but if I ran the same command on command prompt then this is working fine.

But if I execute

new ExecuteCommand(MainActivity.this).execute("ls");

this works fine. Where is the problem in command?

halfer
  • 19,824
  • 17
  • 99
  • 186
Amarjit
  • 4,327
  • 2
  • 34
  • 51
  • [Why does exec() start a ADB daemon?](https://stackoverflow.com/a/34112381/3290339) – Onik Sep 06 '17 at 18:02

3 Answers3

5

adb shell is used when you're executing the command in your PC trying to access the device. But when you are executing the command on device itself, you don't need adb shell

This will be clean:

new ExecuteCommand(MainActivity.this).execute("screencap -p " + f);
Harish Sridharan
  • 1,070
  • 6
  • 10
  • This is not working .Now output is [ 07-31 15:38:11.494 1039:0x502 D/KeyguardViewMediator ] setHidden false – Amarjit Jul 31 '15 at 10:10
  • setHidden false probably is not the problem. This was the only output when you removed the adb shell command? – Thiago Moura Sep 08 '15 at 21:41
  • Logs from KeyguardViewMediator won't impact this. Look elsewhere. Screen record can record even on keyguard, so whether visible or not does not make a difference. – JoxTraex Sep 09 '15 at 21:22
  • Yes , but Screenshot is not saving or may be not taken – Amarjit Sep 11 '15 at 04:28
5

There have been several similar discussions on this, both on this website and android-developers, but the TL:DR is that you cannot really run all shell commands from the device. Most of them need permissions that are only granted to system apps. Using the pm shell commands for example needs the various package permissions, most of which are system-only. You can check the list of permissions here.

You'll either have to root the device, or look for an alternate method of achieving what you want to.

Abhishek
  • 473
  • 3
  • 12
2

I guess there is some issue with the permissions.

Use the new MediaProjection apis to do it.

Refer https://github.com/googlesamples/android-ScreenCapture for source code.

rupesh jain
  • 3,410
  • 1
  • 14
  • 22
  • Thanks!!! But I need to use use something from command line as I have to use other adb commands as well!! – Amarjit Sep 11 '15 at 04:29