2010-07-20 76 views

回答

29

第一情境(可活動/服務等)

您有幾種選擇:

1)使用BundleIntent

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

2)創建新套裝

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

3)使用的意圖

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

注:捆綁有 「得」 與「放「所有原始類型的方法,Parcelables和Serializables。我只是將Strings用於示範目的。

+21

但是,我們不能用getIntent()方法的服務中。當我們從活動向服務發送價值時,如何實現這一點? – 2013-04-07 04:02:00

+0

如何獲得int值... – Prakhar 2013-08-15 07:22:31

+2

這對服務不起作用...? – 2014-04-05 00:59:45

156

對於準確的回答這個問題的「如何從活動中通過故意發送數據到服務」,就是你可以選擇覆蓋是你收到意圖對象onStartCommand()方法:

當您創建一個Service應覆蓋onStartCommand()方法,所以如果你仔細看看下面的簽名,這是你收到它傳遞給它的intent對象:

public int onStartCommand(Intent intent, int flags, int startId) 
從您將創建我的活動

所以ntent對象啓動服務,然後你把你的數據的意圖對象裏面,比如你想傳遞一個UserIDActivityService

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

當服務在該方法中開始了它的onStartCommand()方法將被調用,所以您可以從意向對象檢索值(用戶名),例如

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

注:以上答案指定要獲取與getIntent()方法的意圖這是不正確的服務上下文

+32

這應該是被接受的答案。服務接受的答案是錯誤的。 – zeeshan 2014-07-06 00:40:32

+0

我有這個錯誤:'無法啓動服務意圖:找不到' – fullOfQuestion 2016-07-30 12:07:50

9

如果你綁定你的服務,你會得到額外的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; 
} 
+0

這是否適用於系統服務? – 2017-02-17 19:23:47

+0

@ChefPharaoh這是一個很好的問題。嘗試記錄意圖的值。 'Arrays.toString(yourAry [])'會幫助你。 – 2017-06-08 18:45:48

+1

因爲我想要傳遞一個自定義類,我想通過實現可分區界面,這一切都很好。不過謝謝。 – 2017-06-08 20:52:40

3

另一個posibility使用意圖。的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> 
+0

您正在多次啓動該服務......是否意味着每次都會創建同一服務的多個實例? – oshurmamadov 2017-05-05 05:57:33

+2

服務具有單例模式。 http://stackoverflow.com/questions/2518238/does-startservice-create-a-new-service-instance-or-using-the-existing-one – 2017-05-06 11:31:57

+0

「switch(action)」action can be null – 2018-01-16 09:24:13

1

活動:

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

服務:startservice可引致影響,使用Messenger和傳遞數據最好的辦法。

private CallBackHandler mServiceHandler= new CallBackHandler(this); 
private Messenger mServiceMessenger=null; 
//flag with which the activity sends the data to service 
private static final int DO_SOMETHING=1; 

private static class CallBackHandler extends android.os.Handler { 

private final WeakReference<Service> mService; 

public CallBackHandler(Service service) { 
    mService= new WeakReference<Service>(service); 
} 

public void handleMessage(Message msg) { 
    //Log.d("CallBackHandler","Msg::"+msg); 
    if(DO_SOMETHING==msg.arg1) 
    mSoftKeyService.get().dosomthing() 
} 
} 

活動:從意向獲取Messenger的填充它傳遞數據和傳遞消息發回給服務

private Messenger mServiceMessenger; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
mServiceMessenger = (Messenger)extras.getParcelable("myHandler"); 
} 


private void sendDatatoService(String data){ 
Intent serviceIntent= new 
Intent(BaseActivity.this,Service.class); 
Message msg = Message.obtain(); 
msg.obj =data; 
msg.arg1=Service.DO_SOMETHING; 
mServiceMessenger.send(msg); 
} 
相關問題