sending a parameters from service to activity by intent

1.4k Views Asked by At

how to send a data from service to activity?

when the service is need to open a specific activity from background or even a new instance if this activity is not still at the background (killed).

the question is:

so, how do i open this activity (form what user doing whith phone, and instead received the parameters?

service:

public void sendDataToActivity(int dialog) {
        Intent intent = new Intent(getBaseContext(), MainActivity.class);
        intent .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent .putExtra("dialogToUser", dialog);
        getApplication().startActivity(dialogIntent); 
}

Main-Activity:

@Override
protected void onResume() {
super.onResume();

   //Get Extras and run NumAlert(operation)
   Bundle extras = getIntent().getExtras();
   if (extras.containsKey("dialogToUser") ) {
   int operation = extras.getInt("dialogToUser");
       NumAlert ( operation );
   }
}

it's not working for me, what's wrong??

1

There are 1 best solutions below

1
On

Create a Custom BroadcastReceiver to send data back from service , Here is tutorial link

Or If you want to call the Service from your Activity, what you want is a bound service. It will bind your service to a variable, and you will be able to use that in the Activity.