Send data from Service back to my activity

2019-04-10 12:01发布

I have an activity that creates an intent, puts some extras with putExtra() and calls the startService(intent) to start a service.

This service calculates some stuff based on the extras and then I want to send the result back to the activity.

In which way can I do this?

I tried to create an intent on my service and broadcast it using sendBroadcast(). I have a broadcastReceiver on the activity but Im not sure if I register it correctly. Im confused!

Is there any other way to do so? Something like StartActivityForResult but for services (something like StartServiceForResult or something)?

3条回答
走好不送
2楼-- · 2019-04-10 12:25

The method you are attempting to use is good! Without code though it will be hard to say what you might be doing incorrectly.

In your service you would broadcast an intent like this...

/**
 * Send broadcast to the activity letting it know the service is active
 * 
 * @param pActivate
 */
private final void sendServiceActiveBroadcast( final boolean pActivate ) {
    final Intent _intent = new Intent();
    _intent.setAction( "com.yourtld.android.SERVICE_BROADCAST" );
    _intent.addCategory( "com.yourtld.android.CATEGORY" );
    _intent.putExtra( "isactive", pActivate );

    this.sendBroadcast( _intent );
}

then in your activity you could have something like...

Create an action string:

private static final String SERVICE_BROADCAST_ACTION = "com.yourtld.android.SERVICE_BROADCAST";

In your onResume() method you would have...

final IntentFilter serviceActiveFilter = new IntentFilter( SERVICE_BROADCAST_ACTION );

serviceActiveFilter.addCategory( "com.yourtld.android.CATEGORY" );

this.serviceReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive( final Context context, final Intent intent ) {
        if( intent != null ) {
            if( intent.getBooleanExtra( "isactive", false ) ) {
                // The service is active
            } else {
                // False...
            }
        }
    }
};
this.registerReceiver( this.serviceReceiver, serviceActiveFilter );

Hope that helps

查看更多
在下西门庆
3楼-- · 2019-04-10 12:35

You can do these three things which I know:

  1. You can create an Intent and put data in Intent and start that activity ----- bad Idea
  2. You can use SharedPreferences to pass the values -- only for primitive values
  3. You can use static variables ------ which I think the best way to pass from service.

Note: You can also use DB which is good if u have bulk of data.

查看更多
小情绪 Triste *
4楼-- · 2019-04-10 12:39

First, you can have the service send the intent directly to the activity. That way, even if the activity isn't currently running, it will be launched.

Second, you start the activity with FLAG_ACTIVITY_SINGLE_TOP. This keeps the activity from launching if it's already running.

Third, you implement onNewIntent() in your activity to catch any intents the service sends if your activity is already running.

查看更多
登录 后发表回答