2

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)

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
mobileDev
  • 1,358
  • 2
  • 14
  • 34

3 Answers3

2

Try this:

Intent intent = context.getIntent();
savedNumber = intent.getStringExtra("PHONE_NUMBER");
statosdotcom
  • 3,109
  • 2
  • 17
  • 40
  • I cannot call "this.getIntent" because the code is in "onReceive" method in the receiver and not in activity (edited the last code snipped in my posting) – mobileDev Jul 19 '16 at 02:59
  • @statosdotcom - instead of "this" it should be context – cafebabe1991 Jul 19 '16 at 03:00
  • @kashili , use the context instead of "this" here – cafebabe1991 Jul 19 '16 at 03:01
  • This is what I was begining to write to kas hili. @cafebabe1991, thank you, I didn't write context because the question did not show this before the actual edition. I will edit my answer too. – statosdotcom Jul 19 '16 at 03:02
  • Yes i understand that . – cafebabe1991 Jul 19 '16 at 03:03
  • getIntenet is not part of context, so 'context.getIntent();' throws compiler error, so I casted it to activity as ' ((Activity)context).getIntent().getStringExtra("PHONE_NUMBER");' . Then I ended up with this exception java.lang.ClassCastException: android.app.ReceiverRestrictedContext cannot be cast to android.app.Activity – mobileDev Jul 19 '16 at 03:15
0

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.

Gurjit Singh
  • 1,060
  • 2
  • 13
  • 16
  • No, I cannot start service like you suggested. Because a button's action handler invokes the call intent (multiple times). You are assuming that I know the number 'extras' for intent before starting the service – mobileDev Jul 19 '16 at 03:43
0

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.

Community
  • 1
  • 1
xxx
  • 3,315
  • 5
  • 21
  • 40
  • NEW_OUTGOING_CALL is a protected intent. Hence I am not using NEW_OUTGOING_CALL as you can see in my xml snippet – mobileDev Jul 19 '16 at 03:31
  • Oh so sorry for mistake, ACTION_CALL is also a protected action :) It allow you to call a phone number directly, with no interaction from the user. That's a security risk so it is "protected" by a permission. You'll also notice that Manifest.permission doesn't define that permission publicly: that is intentional within the system. – xxx Jul 19 '16 at 03:41