使用意图将数据从活动传递到服务

如何获取从调用Activity传递的Android服务中的数据?

第一上下文(可以是活动/服务等)

你有几个select:

1)使用意图中的Bundle :

Intent mIntent = new Intent(this, Example.class); Bundle extras = mIntent.getExtras(); extras.putString(key, value); 

2)创build一个新的Bundle

 Intent mIntent = new Intent(this, Example.class); Bundle mBundle = new Bundle(); mBundle.extras.putString(key, value); mIntent.putExtras(mBundle); 

3)使用Intent的putExtra()快捷方法

 Intent mIntent = new Intent(this, Example.class); mIntent.putExtra(key, value); 

新的上下文(可以是活动/服务等)

 Intent myIntent = getIntent(); // this getter is just for example purpose, can differ if (myIntent !=null && myIntent.getExtras()!=null) String value = myIntent.getExtras().getString(key); } 

注意: Bundle对所有的原始types,Parcelables和Serializables都有“get”和“put”方法。 我只是用string作示范。

为了精确地回答“如何通过从活动到服务的意图发送数据”这个问题,是否必须重写onStartCommand()方法,它是您接收意图对象的地方:

当你创build一个Service你应该重写onStartCommand()方法,所以如果仔细看下面的签名,这就是你接收传递给它的intent对象的地方:

  public int onStartCommand(Intent intent, int flags, int startId) 

因此,从活动开始,您将创buildintent对象来启动服务,然后将数据放入intent对象中,例如您想要将ActivityUserID传递给Service

  Intent serviceIntent = new Intent(YourService.class.getName()) serviceIntent.putExtra("UserID", "123456"); context.startService(serviceIntent); 

当服务启动时,它的onStartCommand()方法将被调用,所以在这个方法中,你可以从intent对象中检索值(UserID)

 public int onStartCommand (Intent intent, int flags, int startId) { String userID = intent.getStringExtra("UserID"); return START_STICKY; } 

注意:上面的答案指定使用getIntent()方法得到一个Intent,这个方法在服务上下文中是不正确的

如果你绑定你的服务,你会得到额外的onBind(Intent intent)

活动:

  Intent intent = new Intent(this, LocationService.class); intent.putExtra("tour_name", mTourName); bindService(intent, mServiceConnection, BIND_AUTO_CREATE); 

服务:

 @Override public IBinder onBind(Intent intent) { mTourName = intent.getStringExtra("tour_name"); return mBinder; } 

另一个可能性是使用intent.getAction:

服务中:

 public class SampleService inherits Service{ static final String ACTION_START = "com.yourcompany.yourapp.SampleService.ACTION_START"; static final String ACTION_DO_SOMETHING_1 = "com.yourcompany.yourapp.SampleService.DO_SOMETHING_1"; static final String ACTION_DO_SOMETHING_2 = "com.yourcompany.yourapp.SampleService.DO_SOMETHING_2"; static final String ACTION_STOP_SERVICE = "com.yourcompany.yourapp.SampleService.STOP_SERVICE"; @Override public int onStartCommand(Intent intent, int flags, int startId) { String action = intent.getAction(); //System.out.println("ACTION: "+action); switch (action){ case ACTION_START: startingService(intent.getIntExtra("valueStart",0)); break; case ACTION_DO_SOMETHING_1: int value1,value2; value1=intent.getIntExtra("value1",0); value2=intent.getIntExtra("value2",0); doSomething1(value1,value2); break; case ACTION_DO_SOMETHING_2: value1=intent.getIntExtra("value1",0); value2=intent.getIntExtra("value2",0); doSomething2(value1,value2); break; case ACTION_STOP_SERVICE: stopService(); break; } return START_STICKY; } public void startingService(int value){ //calling when start } public void doSomething1(int value1, int value2){ //... } public void doSomething2(int value1, int value2){ //... } public void stopService(){ //...destroy/release objects stopself(); } } 

在活动中:

 public void startService(int value){ Intent myIntent = new Intent(SampleService.ACTION_START); myIntent.putExtra("valueStart",value); startService(myIntent); } public void serviceDoSomething1(int value1, int value2){ Intent myIntent = new Intent(SampleService.ACTION_DO_SOMETHING_1); myIntent.putExtra("value1",value1); myIntent.putExtra("value2",value2); startService(myIntent); } public void serviceDoSomething2(int value1, int value2){ Intent myIntent = new Intent(SampleService.ACTION_DO_SOMETHING_2); myIntent.putExtra("value1",value1); myIntent.putExtra("value2",value2); startService(myIntent); } public void endService(){ Intent myIntent = new Intent(SampleService.STOP_SERVICE); startService(myIntent); } 

最后,在清单文件中:

 <service android:name=".SampleService"> <intent-filter> <action android:name="com.yourcompany.yourapp.SampleService.ACTION_START"/> <action android:name="com.yourcompany.yourapp.SampleService.DO_SOMETHING_1"/> <action android:name="com.yourcompany.yourapp.SampleService.DO_SOMETHING_2"/> <action android:name="com.yourcompany.yourapp.SampleService.STOP_SERVICE"/> </intent-filter> </service> 

活动:

 int number = 5; Intent i = new Intent(this, MyService.class); i.putExtra("MyNumber", number); startService(i); 

服务:

 @Override public int onStartCommand(Intent intent, int flags, int startId) { if (intent != null && intent.getExtras() != null){ int number = intent.getIntExtra("MyNumber", 0); } }