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); } }