简体   繁体   English

将服务中的数据发送回我的活动

[英]Send data from Service back to my activity

I have an activity that creates an intent, puts some extras with putExtra() and calls the startService(intent) to start a service. 我有一个创建意图的活动,使用putExtra()添加一些额外内容并调用startService(intent)来启动服务。

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(). 我尝试在我的服务上创建一个intent并使用sendBroadcast()进行广播。 I have a broadcastReceiver on the activity but Im not sure if I register it correctly. 我有一个关于活动的broadcastReceiver,但我不确定我是否正确注册。 Im confused! 我糊涂了!

Is there any other way to do so? 有没有其他方法可以这样做? Something like StartActivityForResult but for services (something like StartServiceForResult or something)? 像StartActivityForResult,但对于服务(像StartServiceForResult或类似的东西)?

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... 在你的onResume()方法中你会...

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 希望有所帮助

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 . 其次,使用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. 第三,在活动中实现onNewIntent()以捕获服务在您的活动已经运行时发送的任何意图。

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 您可以创建一个Intent并将数据放入Intent并启动该活动-----糟糕的想法
  2. You can use SharedPreferences to pass the values -- only for primitive values 您可以使用SharedPreferences传递值 - 仅适用于原始值
  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. 注意:如果您有大量数据,也可以使用DB。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM