3

For some time now I'm trying to pass the simple one String data from the Service to the Activity with Intent.putExtra() but with no success however. Intent.getStringExtra() is always NULL

SERVICE CODE:

Intent intent=new Intent(getBaseContext(),MainActivity.class);
intent.putExtra(Consts.INTERNET_ERROR, "error");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(intent);

ACTIVITY CODE:

public void onResume() {

    super.onResume();

    Intent i = getIntent();
    String test = "temp";
    Bundle b = i.getExtras();
    if (b != null) {
        test = b.getString(Consts.INTERNET_ERROR);
    }
}   

Any suggestions?

Nikhil Agrawal
  • 23,885
  • 19
  • 85
  • 117
Majstor
  • 839
  • 1
  • 12
  • 22
  • You have to implement onNewIntent, the intent in your service is sent to your activity through onNewIntent. – Hoan Nguyen Feb 05 '13 at 01:16
  • Just call [getIntent().getStringExtra(String)](http://developer.android.com/reference/android/content/Intent.html#getStringExtra(java.lang.String)). You didn't put a bundle into the intent. –  Feb 05 '13 at 02:03

4 Answers4

6

To elaborate my comment above, getIntent returns the original intent as documented at [1]http://developer.android.com/reference/android/app/Activity.html#onNewIntent(android.content.Intent) [1]:

The intent from the service is passed to your activity through onNewIntent which is called before onResume. Thus if you override onNewIntent you will get the intended string.

@Override
protected void onNewIntent(Intent intent)
{
    super.onNewIntent(intent);

    // set the string passed from the service to the original intent
       setIntent(intent);

}

Then your code onResume will work.

Hoan Nguyen
  • 17,479
  • 3
  • 47
  • 51
  • Hmm sounds promising.. Will try now – Majstor Feb 05 '13 at 08:23
  • Works!! Great.. Tnx man.I dont know how I didn't thought on that, as I used onNewIntent few times till now.. Doesn't mater now. Thank you very much. – Majstor Feb 05 '13 at 08:30
  • this also worked for me when i needed to pass back from current activity to an activity that on pause – Waqleh Oct 01 '13 at 13:00
0

I don't see where you add the String in the service code, however you can run the following code to send the string.

Intent intent=new Intent(getBaseContext(),MainActivity.class);
Bundle b = new Bundle();
b.putString(Consts.INTERNET_ERROR, "Your String Here");
intent.putExtras(b);
intent.setAction(Consts.INTERNET_ERROR);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(intent);
muhoweb
  • 106
  • 3
  • When using your solution in Activity code `Bundle b=i.getExtras();` I got `NULL`. Tested right now... – Majstor Feb 04 '13 at 23:01
0

First: try to move your code from onResume to onStart:

public void onStart() {

    super.onStart();

    Intent i = getIntent();
    String test = "temp";
    Bundle b = i.getExtras();
    if (b != null) {
        test = b.getString(Consts.INTERNET_ERROR);
    }
}  

If it still not working try with this:

First activity:

Intent myIntent = new Intent(firstActivity.this, secondActivity.class);
myIntent.putExtra("mystring",strValue)' <=your String
startActivity(myIntent);

Second activity:

 String str = getIntent.getExtras().getString("mystring");<=get string 

and check here for some informations:

How do I pass data between Activities in Android application?

Community
  • 1
  • 1
Augusto Picciani
  • 718
  • 1
  • 10
  • 31
0

Rather than calling

extras.getString();

Try calling:

intent.getStringExtra("key");

Also, are you starting the activity directly FROM the service? Or are you already running on the activity and simply want to receive data from the service?

daniel_c05
  • 10,970
  • 15
  • 57
  • 76