0

I am executing shell commands in code which does a specific job in background. It can last anywhere from 1 - 100 secs.

Ex : adb shell sleep 100

How do I terminate execution of executed commands programmatically, similar to pressing CTRL+C in PC.

What have I tried :

Initiate

 Process mPrcs = Runtime.getRuntime().exec(SUPER_USER);
 DataOutputStream sOutputStream = null;
 sOutputStream = new DataOutputStream(mPrcs.getOutputStream());

On Start

public void StartClicked(View v)
{
     sOutputStream.writeBytes("adb shell sleep 100\n");
     sOutputStream.flush();
}

On Stop, execute exit

 sOutputStream.writeBytes("exit\n");
 sOutputStream.flush();

Destroy()

mPrcs.destroy();

KillProcess using PID, app get's killed but the executed code doesn't stop executing.

Process.killProcess(Process.myPid());

None of the above seems to stop.

Also is there a way to find when executed code is completed?
Process.waitfor() makes my app to hang & goes to not responding state.

Community
  • 1
  • 1
VenomVendor
  • 15,064
  • 13
  • 65
  • 96
  • This doesn't make sense "adb shell" is a command from a host to a device, so don't issue it from a lifecycle method of code on the device. Instead just issue the sleep. To kill a process you need its pid, not your own. Also note root is not required unless the command itself requires it. You may not be able to kill a subprocess running as root except from another one. – Chris Stratton Nov 04 '14 at 13:54
  • I am trying to use `KitKat` [screen recording API](http://developer.android.com/about/versions/kitkat.html#44-media), am able to start with `adb shell screenrecord` If user want's to stop. How do I stop it? – VenomVendor Nov 04 '14 at 15:45
  • Again, that is the wrong command to issue from code on the device. You are all mixed up about what you are doing here, and solving your problem requires untangling that to a clear picture. – Chris Stratton Nov 04 '14 at 15:53
  • Am trying to create an app, that records the screen in KitKat devices using `adb shell screenrecord`, when I execute the command. I need to stop recording, when user interrupts which am not able to achieve. – VenomVendor Nov 04 '14 at 15:57
  • And again "adb shell" anything **is not the way for the device to do anything to itself** - adb is for interacting with **remote** systems. This represents **fundamental confusion** that is getting in the way of solving your problem. You need to understand how all of the pieces fit together if you are going to try to do unsupported things with them. – Chris Stratton Nov 04 '14 at 16:03
  • makes sense, but apart from my way of solving problem or achieving something, the above question has a problem stated. Is there a solution for that? – VenomVendor Nov 04 '14 at 16:06
  • No, you don't really have a problem statement - you have a mess of confusion. When you take time to sort out the mess, you'll be on the path towards identifying and solving the actual problem, **through an understanding of what processes running as what users on what systems are involved, and how they are connected**. – Chris Stratton Nov 04 '14 at 16:09

0 Answers0