ADB :) ADB shell AM

Source: Internet
Author: User

You can use the am command to start an existing app in the system in the command line mode.


The ADB shell enters the command line.


Press enter to view the help information about AM.


The meaning of each parameter is not described here. Just give a few examples.


To illustrate the problem, write an app, an activity, and a service:




Source code:

package mark.zhang;import android.app.Activity;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.os.Handler;public class AmActivity extends Activity {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);IntentFilter mFilter = new IntentFilter();mFilter.addAction("action_finish");registerReceiver(listener, mFilter);}private BroadcastReceiver listener = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();if ("action_finish".equals(action)) {new Handler().postDelayed(new Runnable() {@Overridepublic void run() {finish();}}, 5000);}}};protected void onDestroy() {super.onDestroy();unregisterReceiver(listener);};}

package mark.zhang;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.util.Log;public class MyService extends Service {@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn null;}@Overridepublic void onCreate() {super.onCreate();Log.d("mark", "MyService--- onCreate callbacks");}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.d("mark", "MyService--- onStartCommand callbacks");return super.onStartCommand(intent, flags, startId);}}

Receive the broadcast in amactivity and finish after the 5S.


Install the compiled APK of this project to the simulator:


ADB install **/am.apk


1. Start Activity


Am start-N mark. Zhang/mark. Zhang. amactivity


The activity is started at this time.


2. Send Broadcast


AM broadcast-a "action_finish"


After the 5S, you can see that amactivity is finished.


3. Start the service


Am startservice-N mark. Zhang/mark. Zhang. myservice


Print Information



Source code


Framworks/base/cmds/AM/src/COM/Android/commands/AM. Java


Program entry


 /**     * Command-line entry point.     *     * @param args The command-line arguments     */    public static void main(String[] args) {        try {            (new Am()).run(args);        } catch (IllegalArgumentException e) {            showUsage();            System.err.println("Error: " + e.getMessage());        } catch (Exception e) {            System.err.println(e.toString());            System.exit(1);        }    }





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.