参考 https://gist.github.com/burinov/d617be5eadc60d4021e3de6841e04c7c
#Example on how to use adb to start an Activity, #BroadcastReceiver or Service from adb and include intent extras too. #for Activity: adb shell am start -n "com.peirr.test/com.peirr.test.MyActivity" --es name "John" --ei age 30 #for BroadcastReceiver adb shell am broadcast -n "com.peirr.test/com.peirr.test.MyReceiver" --es name "John" --ei age 30 #for Service adb shell am startservice -n "com.peirr.test/com.peirr.test.MyService" --es name "John" --ei age 30
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.peirr.test"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"> <receiver android:name=".MyReceiver" android:enabled="true" android:exported="true" /> <service android:name=".MyService" android:enabled="true" android:exported="true" /> <activity android:name=".MyActivity" android:exported="true" /> </application> </manifest>
MyActivity.java
package com.peirr.test; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; public class MyActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); TextView lname = (TextView) findViewById(R.id.name); TextView lage = (TextView) findViewById(R.id.age); Intent intent = getIntent(); if (intent != null) { String name = intent.getStringExtra("name"); int age = intent.getIntExtra("age",0); lname.setText(name); lage.setText(String.valueOf(age)); } } }
MyReceiver.java
package com.peirr.test; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class MyReceiver extends BroadcastReceiver { public MyReceiver() {} @Override public void onReceive(Context context, Intent intent) { if (intent != null) { String name = intent.getStringExtra("name"); int age = intent.getIntExtra("age",0); Toast.makeText(context,"MyReceiver Received: [name:"+name+"] [age:"+age+"]",Toast.LENGTH_SHORT).show(); } } }
MyService.java
package com.peirr.test; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.widget.Toast; public class MyService extends Service { public MyService() {} @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. throw new UnsupportedOperationException("Not yet implemented"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { if (intent != null) { String name = intent.getStringExtra("name"); int age = intent.getIntExtra("age",0); Toast.makeText(this, "MyService Received: [name:" + name + "] [age:" + age + "]", Toast.LENGTH_SHORT).show(); } return super.onStartCommand(intent, flags, startId); } }