user2545386
user2545386

Reputation: 457

How to use adb shell controll android app?

I am trying to develop an android demo.I want to use command in adb shell to make the demo do what I want.I may need an shell file.

The result I want like this.

root@device:/ # <shell> log "hello world" 

then my app console "hello world" at logcat.

but I don't know how to connect them.

Upvotes: 1

Views: 2056

Answers (2)

davidgiga1993
davidgiga1993

Reputation: 2853

Just to clarify: You want to send commands from adb shell (local android shell) to your app? That's not possible without quite a few lines of code, and possible requires rooting of the device.

You'd be better of by just adding an UI to the app where you can call events.

Edit: As m0skit0 pointed out it is possible to send intents via commandline. This is definitely the cleanest solution.

Upvotes: 0

m0skit0
m0skit0

Reputation: 25863

You can send Intents to your app through the shell, although your app has to register the appropriate BroadcastListener. Here's a simple example:

public class MyActivity extends Activity {

    private static final String TAG = MyActivity.class.getSimpleName();

    public static final String INTENT_FILTER = "com.example.myapp.TEST";
    public static final String EXTRA_KEY = "new_text";

    private class MyIntentReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(final Context context, final Intent intent) {
            final String newText = intent.getExtras().getString(EXTRA_KEY);
            Log.d(TAG, "Received new text: " + newText);
        }
    }

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        registerReceiver(new MyIntentReceiver(), new IntentFilter(INTENT_FILTER));
    }
}

Then you can do in shell:

am broadcast -a com.example.myapp.TEST -e "new_text" "New text"

And you will see how your app receives and logs it.

Upvotes: 4

Related Questions