androidbroadcastreceivercalltelephonyphone-state-listener

Incoming number during a call in android?


I am running following code, its compiling but I am not getting any result or toast displayed please help...

CustomBroadcastReceiver.java this class will receive the action phone state change and will instantiate the customephonestatelistener

public class CustomBroadcastReceiver extends BroadcastReceiver {

TelephonyManager telephony = 
(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);

CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener(context);

telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);


Bundle bundle = intent.getExtras();
String phoneNr= bundle.getString("incoming_number");
    Log.v(TAG, "phoneNr: "+phoneNr);
    Toast toast = Toast.makeText(context, "phoneNr: "+phoneNr, Toast.LENGTH_LONG);
    toast.show();
}
}

CustomPhonestateListener.java

This class is main operating class

public class CustomPhoneStateListener extends PhoneStateListener {

private static final String TAG = "CustomPhoneStateListener";

private Context mContext;

public CustomPhoneStateListener(Context context) {


// TODO Auto-generated constructor stub
mContext = context;
}

public void onCallStateChanged(int state, String incomingNumber){

    Log.v(TAG, "WE ARE INSIDE!!!!!!!!!!!");

     Log.v(TAG, incomingNumber);
    Toast toast = Toast.makeText(mContext, "WE ARE INSIDE!!!!!!!!!!!", Toast.LENGTH_LONG);
    toast.show();

    switch(state){
            case TelephonyManager.CALL_STATE_RINGING:
                    Log.d(TAG, "RINGING");
                    break;
    }       

AndroidManifest.xml

  <uses-permission android:name="android.permission.RECEIVE_SMS" />
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.WRITE_CONTACTS" />
  <uses-permission android:name="android.permission.READ_PHONE_STATE" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <receiver android:name=".MyBroadcastReceiver">
            <intent-filter>
                    <action android:name="android.intent.action.PHONE_STATE" />

            </intent-filter>
    </receiver>
</application>

Solution

  • "But now the problem is I have to reboot the device in order to start this service..any suggestion where I am being wrong now??"

    The reason that you have to restart your phone is that in your AndroidManifest.xml file you have allowed for only one action...

    <!-- Your Problem is here -->
    <action android:name="android.intent.action.BOOT_COMPLETED" />
    

    You have set your BroadcastReceiver to only start when the phone is BOOTED.
    To fix this, you need to use the answer provided by DR VOLT.

    Just add the actions PHONE_STATE and NEW_OUTGOING_CALL to your receivers
    For Example:

    <receiver android:name=".MyReceiver" >
    <intent-filter>
    <action android:name="android.intent.action.PHONE_STATE" />
    <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
    <action android:name="android.intent.action.BOOT_COMPLETED" />           
    </intent-filter>
    </receiver>
    

    This will then start your broadcast receiver whenever these actions occur.