I am retrieving the outgoing number from the broadcast receiver and trying to send it to the activity thru a method getNumber() however the value is coming out null. Im my code below In the activity class the String phonenumber is null
BroadcastReciever Class:
public class OutgoingBroadcastReceiver extends BroadcastReceiver {
String phonenumber = null;
@Override
public void onReceive(Context context, Intent intent) {
phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL"))
{
Log.i("System out", "IN OUTGOING CALL......... :IF");
MyPhoneStateListener phoneListener = new MyPhoneStateListener(
context);
TelephonyManager telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener,
PhoneStateListener.LISTEN_CALL_STATE);
} else {
Log.i("System out", "IN INCOMING CALL.........:else:receiver");
}
public String getNumber()
{
return phonenumber;
}
Activity Class:
public class OutgoingCallScreenDisplay extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.outgoing_main);
OutgoingBroadcastReceiver outreciever = new OutgoingBroadcastReceiver();
String phonenumber= outreciever.getNumber();//this is coming out NULL needs to be the outgoing number
}
Phone no. is null because u r making a new instance of broadcastreceiver whose values are not initilized. You can receive a phone no. in “onReceive” ONLY ,from there u should start a new activity if u want to pass it further
See:
Firstly you shud either use a broadcast receiver or a phone state listener. In ur case if u r using a broadcast receiver then u simple need intent extra:EXTRA_PHONE_NUMBER
and permission : process outgoin call .That’s it…..