mobileDev
mobileDev

Reputation: 1464

Missing 'extras' data in intent

I am sending intent from "activity" to a receiver in "service" (and pass the data). My code has activity and service (that has reciever). Receiver is declared as follows

 <receiver android:name="xxx"
        android:enabled="true"  >
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
           <!-- protected intents meant for os and not for us <action android:name="android.intent.action.ACTION_NEW_OUTGOING_CALL" android:priority="0" /> -->
        </intent-filter>

  </receiver>

Activity is defined as follows

   <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:launchMode="singleTask"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

I reviewed Use an intent to send data to my activity

Intent I am invoking is the call intent and passing the destination call number, with the following code

    Log.e(TAG,"Calling "+number);
    Intent callIntent = new Intent(Intent.ACTION_CALL); //ACTION_NEW_OUTGOING_CALL is deprecated in API 21, hence ACTION_CALL          
    callIntent.putExtra("PHONE_NUMBER",number);
    number = "tel:"+number;
    callIntent.setData(Uri.parse(number));
    startActivity(callIntent);

Above code successfully makes a telephone call from my app. I also have a receiver to intercept the calls and the reciever intercepts the above call just fine. However 'extras' of above intent is missing in the receiver; I always get "PHONE_NUMBER" as null in the following code

@Override
public void onReceive(Context context, Intent intent) {
      //blah blah..
 savedNumber = intent.getExtras().getString("PHONE_NUMBER");
 if(savedNumber == null)
        savedNumber = intent.getStringExtra("PHONE_NUMBER");
 Log.e(TAG, " savedNumber = "+savedNumber);
}

What is my mistake and why is that I get the intent in the reciever but the 'extras' is missing (as you may have noticed, I tried to get it both ways from intent)

Upvotes: 2

Views: 1594

Answers (3)

SGX
SGX

Reputation: 3363

getIntent() is method of Activity class. You can see in the onReceive() method has an intent argument, you get string from this.

String number = null;
number = intent.getStringExtra("PHONE_NUMBER");

But i read on this article: How to pass Extra to BroadcastReceiver, when initiating ACTION_CALL Only the Android system itself can broadcast the NEW_OUTGOING_CALL Intent. You can't add your own extras to this Intent. You'll need to come up with another way to do whatever it is you are trying to accomplish.

Upvotes: 0

Gurjit Singh
Gurjit Singh

Reputation: 1120

Start service like this-

Intent callIntent=new Intent(this, Service.class); 
callIntent.putExtra("phonenumber",number);
this.startService(callIntent); 

Then retrieve data from the service;

data=(String) intent.getExtras().get("phonenumber"); 

You can access your parameter from either the onHandleIntent or onStartCommand Intent parameter.

Service

protected void onStartCommand (Intent intent, int flags, int startId) {
data=(String) intent.getExtras().get("data"); 
}

IntentService

protected void onHandleIntent(Intent intent) {
data=(String) intent.getExtras().get("data"); 
}

It depends on which type of service you are running.

Upvotes: 0

Try this:

Intent intent = context.getIntent();
savedNumber = intent.getStringExtra("PHONE_NUMBER");

Upvotes: 2

Related Questions