2010-07-20 94 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); 
}