Sharanabasu Angadi
Sharanabasu Angadi

Reputation: 4372

get the value from Intent of android

Thanks in advance. When I print Log.d("me",getIntent().toString());

I am getting:

Intent { act=android.intent.action.CALL_PRIVILEGED dat=tel:888 flg=0x13800000 cmp=com.ninetology.freecall.two/.CallFinalActivity }

I am trying to fetch the value which is associated with "dat" but I am getting NullPointer exception.

//the code I am using is
getIntent().getStringExtra("dat"); // no use
//i tried 
getIntent().getExtras("dat").toString(); // NullPointer exception

I tried with "tel" as key in above code still no use.

Upvotes: 2

Views: 11002

Answers (4)

thepoosh
thepoosh

Reputation: 12587

it seems you're doing this wrong.

  1. The getExtras() function returns a bundle that you can extract data from and not a function that returns a specific String.

  2. dat is NOT a String value as you can see from the data that was printed. it's a Uri,

try parsing it as you should and I'm sure you'll be able to get the data.

public void onCreate(Bundle b) { //mistyped
    super.onCreate(b);
    Uri data = getIntent().getData();
    // OR USE THIS
    String data = getIntent().getDataString();
    // DO STUFF
}

Upvotes: 4

user370305
user370305

Reputation: 109237

First of all, Its not necessary the string from Intent your are getting in log have a object with values..

So its better to just check its not a null, like,

Bundle bundle = getIntent().getExtras();

if(bundle ! = null)
{
  // Now check you bundle object which has a values or not  
}
else
{
  // 1. get data in form of Uri
  Uri data = getIntent().getData();

  // 2. OR get string of Uri
  String dataString = getIntent().getDataString();

  // 3. Or split the data string

  // The logic from this part may be different on your requirement.. I only suggests you to get data from string.. (Actual logic may different on your case)
  String data = getIntent().toString();
  data = data.subString(data.indexOf(":"), data.indexOf("flg")-1);
  Log.e("tel:", data);
}

Upvotes: 1

AkashG
AkashG

Reputation: 7888

When you want to pass the data with the intent just add the below code before starting activity

intent.putExtra("dat", value); //value=the value you want to send

And when you want to fetch the same value in another activity just do:

Bundle bundle=getIntent().getExtras();
if(bundle!=null){
      String string=bundle.getString("dat");
       }

By doing this, you wont get the null pointer exception and will help you.

Upvotes: 0

Mohsin Naeem
Mohsin Naeem

Reputation: 12642

try getIntent().getExtras().get("dat");

Upvotes: 1

Related Questions