2

I have a shell script that looks does the following:

# /mnt/sdcard/touch.sh
sleep 10

while [ 1 ]; do
    input swipe 700 380 1020 600 1000
    input tap 960 540
    input swipe 1220 700 900 480 1000
    input tap 960 540
    input swipe 1220 700 900 480 1000
    input tap 960 540
    input swipe 700 380 1020 600 1000
    input tap 960 540
done

If I execute it from Terminal IDE (a terminal emulator), it only works while Terminal IDE is in the foreground, so I thought I'd make an actual app to execute it and keep it running in the background. Here's what I've got now:

package com.mycompany.myapp;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import java.io.*;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {
            // RIGHT HERE. WHY DOES NOTHING HAPPEN?
            Runtime.getRuntime().exec(new String[]{"/system/bin/sh", "/mnt/sdcard/touch.sh", });
        }
        catch (IOException e) {/*...*/}
    }
}

But it doesn't do anything.

So how would I execute this, then leave it running in the background so that the taps and swipes happen while another app is in the foreground? You get the idea. :D

  • Activities don't run in the background: only services do. We're getting from automation into app programming now, and reaching the limits of what's suitable for this site, so I'll point you at the official documentation. – Dan Hulme May 10 '14 at 8:26
  • @geffchang I am an android enthusiast and running this script is what I want to do. – trusktr May 18 '14 at 0:57
  • This question shouldn't be closed as offtopic. He just tried to solve it via writing app, but his question isn't about it at all. – janot Aug 12 '14 at 6:44
4

It worked by just doing sh /mnt/sdcard/touch.sh & after having started a shell with adb shell. Then, when I closed the terminal forcibly while the process was in the background, the process remained running in the background of my phone as expected. I was able to kill the process later by getting with adb shell the using ps to find the process ID and kill -s KILL <ID> to kill it.

Basically, turns out I didn't have to make an Android app just to make my script run in the background. Clash on...

  • I'm curious what various versions of Android behavior is. I wrote a script to log battery levels that runs in an endless loop - and I find if I start forked like you did & - and pull the USB cable... that it in fact halts... but resumes if I return USB cable. – RoundSparrow hilltx Dec 6 '14 at 17:45
  • had the same needs, answer helped me greatly! but... oddly enough, only within adb shell I could get to do it with no root. when running the exact same script within an android terminal app, I could only achieve the same effect (to actually get some touching working) after activating root with su! any clues on why this could be? – cregox Apr 21 '17 at 21:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.