-2

I need to communicate between activity and service. I have a service that is running in background.

  • When activity starts it will check the service is running or not.
  • If not running then activity will start the service.
  • If already running then activity will request service for some data.

I need to get update data from service when I want just like polling method. Actually after certain period I need data from service.

How I can get data from service?

As far as I know it can be done by binding service. But I don't want to bind the service because I need to run the service when activity isn't on focus.

madlymad
  • 6,367
  • 6
  • 37
  • 68
Abu Yousuf
  • 5,729
  • 3
  • 31
  • 50
  • Why don't you go here: http://developer.android.com/guide/components/services.html – Andy Dec 28 '14 at 13:52
  • I read this . I also read tutorial about activity and service inter communication. Those tutorial shows communication from service to activity by sending broadcast but i need reverse communication from activity to service . – Abu Yousuf Dec 28 '14 at 14:53

2 Answers2

2

Use BroadcastReceivers. In the activity declare the Broadcastreceiver:

public static final String ACTION_MY_ACTION = "com.example.test.MY_ACTION";

private BroadcastReceiver my_broadcast_receiver = new BroadcastReceiver()
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        //extract the data from intent here
        String data = intent.getStringExtra("my_key");
    }
}


@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    IntentFilter filter = new IntentFilter(ACTION_MY_ACTION);

    //we register the receiver
    registerReceiver(my_broadcast_receiver, filter);
}

@Override
protected void onDestroy()
{
    super.onDestroy();
    unregisterReceiver(my_broadcast_receiver);
}

In the service whenever you want to send data to the activity:

Intent i = new Intent(MyActivity.ACTION_MY_ACTION);
i.putExtra("my_key", "data");
sendBroadcast(i);

EDIT:

If its a concern that other apps can intercept the broadcast then use LocalBroadcastManager, and send a local broadcast, this way the data you are broadcasting won't leave your app, so don't need to worry about leaking private data.

Goran Horia Mihail
  • 3,536
  • 2
  • 29
  • 40
0

I will attempt to lead you in the right direction. My suggestion would be to update your apps background datas from the service (like database, settings, etc.). The other thing I notice is you might be trying to update views for you to want to be doing this. This question should be helpful on how to start your activity from your Service. I wouldn't recommend doing that. Instead, check if your Activity is running and make updates then. Finding out how to do that is a simple google search.

Community
  • 1
  • 1
Andy
  • 10,553
  • 21
  • 75
  • 125
  • I don't need to start activity from service. I just need data from service. If there any process that activity can call a function or request to service for data that will be fine. Thanks for your answer – Abu Yousuf Dec 28 '14 at 15:34
  • In that case you have access to most of the data entry points you would have in an Activity. For example, you can open the connection to the SQLite database helper, etc. – Andy Dec 28 '14 at 15:37