使用Intent将数据从Activity传递到Service

Gob*_*ffi 62 service android android-intent android-activity

如何获取Service从调用传递的Android 中的数据Activity

use*_*_CC 197

有关"如何通过意图从活动向服务发送数据"这一问题的精确答案,您是否必须覆盖onStartCommand()接收意图对象的方法:

当你创建一个时,Service你应该覆盖该onStartCommand()方法,所以如果你仔细查看下面的签名,这就是你收到intent传递给它的对象的地方:

  public int onStartCommand(Intent intent, int flags, int startId)
Run Code Online (Sandbox Code Playgroud)

因此,从一个活动中,您将创建intent对象以启动服务,然后将数据放在intent对象中,例如,您希望将UserIDfrom 传递ActivityService:

 Intent serviceIntent = new Intent(YourService.class.getName())
 serviceIntent.putExtra("UserID", "123456");
 context.startService(serviceIntent);
Run Code Online (Sandbox Code Playgroud)

当服务启动时,onStartCommand()将调用其方法,因此在此方法中,您可以从intent对象中检索值(UserID)

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

注意:上面的答案指定使用getIntent()在服务上下文中不正确的方法获取Intent

  • 这应该是公认的答案.接受的答案对于服务是错误的. (45认同)
  • 服务启动后如何向服务发送意图? (2认同)

Pen*_*m10 30

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

你有几个选择:

1)使用意向:

Override
public int onStartCommand(Intent intent, int flags, int startId) {
Run Code Online (Sandbox Code Playgroud)

2)创建一个新的Bundle

Intent mIntent = new Intent(this, Example.class);
Bundle extras = mIntent.getExtras();
extras.putString(key, value);  
Run Code Online (Sandbox Code Playgroud)

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

Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.extras.putString(key, value);
mIntent.putExtras(mBundle);
Run Code Online (Sandbox Code Playgroud)

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

Intent mIntent = new Intent(this, Example.class);
mIntent.putExtra(key, value);
Run Code Online (Sandbox Code Playgroud)

注意: Bundles对所有基本类型,Parcelables和Serializables都有"get"和"put"方法.我只是将Strings用于演示目的.

  • 但是我们不能在服务中使用getIntent()方法.当我们从活动向服务发送价值时,如何实现这一目标? (27认同)
  • getIntent()不适用于服务 (5认同)
  • 这对服务不起作用......? (4认同)
  • 只需使用intent就不需要getIntent()了 (2认同)
  • 在onStartCommand方法中使用意图 (2认同)
  • 这不应该是公认的答案,因为这不是正确的服务解决方案。 (2认同)

Mar*_*fer 9

如果您绑定了您的服务,您将获得Extra in onBind(Intent intent).

活动:

 Intent intent = new Intent(this, LocationService.class);                                                                                     
 intent.putExtra("tour_name", mTourName);                    
 bindService(intent, mServiceConnection, BIND_AUTO_CREATE); 
Run Code Online (Sandbox Code Playgroud)

服务:

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


Car*_*mez 5

另一个可能性是使用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();
    }
}
Run Code Online (Sandbox Code Playgroud)

在活动中:

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);
}
Run Code Online (Sandbox Code Playgroud)

最后,在Manifest文件中:

<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>
Run Code Online (Sandbox Code Playgroud)

  • 服务有单身模式.http://stackoverflow.com/questions/2518238/does-startservice-create-a-new-service-instance-or-using-the-existing-one (3认同)