Incoming number during a call in android?

淺唱寂寞╮ 提交于 2019-12-04 17:41:23
Robbie

"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.

Thnx for the answers...I tried another approach and following code seems to work..Joined both receiver and phonestatelistener..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??

public class IncomingCallReciever extends BroadcastReceiver {

private Context mContext;
private Intent mIntent;

@Override
public void onReceive(Context context, Intent intent) {
    mContext = context;
    mIntent = intent;
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    int events = PhoneStateListener.LISTEN_CALL_STATE;
    tm.listen(phoneStateListener, events);
}

private final PhoneStateListener phoneStateListener = new PhoneStateListener() {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        String callState = "UNKNOWN";
        switch (state) {
        case TelephonyManager.CALL_STATE_IDLE:
            callState = "IDLE";
            break;
        case TelephonyManager.CALL_STATE_RINGING:
            // -- check international call or not.
            if (incomingNumber.startsWith("00")) {
                Toast.makeText(mContext,"International Call- " + incomingNumber,Toast.LENGTH_LONG).show();
                callState = "International - Ringing (" + incomingNumber+ ")";
            } else {
                Toast.makeText(mContext, "Local Call - " + incomingNumber, Toast.LENGTH_LONG).show();
                callState = "Local - Ringing (" + incomingNumber + ")";
            }
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            String dialingNumber = mIntent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
            if (dialingNumber.startsWith("00")) {
                Toast.makeText(mContext,"International - " + dialingNumber,Toast.LENGTH_LONG).show();
                callState = "International - Dialing (" + dialingNumber+ ")";
            } else {
                Toast.makeText(mContext, "Local Call - " + dialingNumber,Toast.LENGTH_LONG).show();
                callState = "Local - Dialing (" + dialingNumber + ")";
            }
            break;
        }
        Log.i(">>>Broadcast", "onCallStateChanged " + callState);
        super.onCallStateChanged(state, incomingNumber);
    }
};

}
TGIO

The problem is in your manifest file, you must have this:

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

instead of :

<receiver android:name=".MyReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />                
</intent-filter>
</receiver>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!