How can getCallerID in android by programatically?
I am making android apps we need caller开发者_Go百科ID ,How can proceed it i.e. when any user call from android device we want find out callerID. Thanks.
You can implement a BroadcastReceiver and deal with telephony events like this:
public class CallListener extends BroadcastReceiver {
private static final String OUTGOING_CALL_ACTION = Intent.ACTION_NEW_OUTGOING_CALL;
private static boolean isOutgoingCall;
private static String savedNumber;
@Override
public void onReceive(Context context, Intent intent) {
int event = -1;
String action = intent.getAction();
if (action.equals(OUTGOING_CALL_ACTION)) {
// get phone number from bundle
savedNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
isOutgoingCall = true;
}
else
{
//gets phone's state
String phoneState = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if(phoneState.equals(TelephonyManager.EXTRA_STATE_RINGING))
{
isOutgoingCall = false;
//gets the phone number of this incoming call
savedNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.d(TAG, String.format("Received phone call from [%s]", savedNumber));
}
else if(phoneState.equals(TelephonyManager.EXTRA_STATE_IDLE))
{
// call ended
Log.i(TAG, TelephonyManager.EXTRA_STATE_IDLE);
savedNumber = null;
event = InCallManager.CALL_STATE.EVENT_IDLE;
}
else if(phoneState.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
{
// call answered
if(isOutgoingCall) {
event = InCallManager.CALL_STATE.EVENT_OUTCALL;
isOutgoingCall = false;
}
else {
event = InCallManager.CALL_STATE.EVENT_INCALL;
}
}
}
}
}
in your manifest just add:
<receiver android:name="receivers.CallListener">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
You should use a Broad cast receiver as follows:
Register your broadcast receiver in manifest file as follows:
<receiver android:name="MyOwnBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
and next step is to implement it in your code which can be done as follows
public class MyOwnBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
String INCOMING_NUMBER = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.e("incoming phone number is",""+INCOMING_NUMBER);
}
}
create a broadcast receiver class and use incoming intent....
public class Sms_Res extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String num=null;
num=intent.getStringExtra("incoming_number");
Toast.makeText(context,num, Toast.LENGTH_LONG).show();
}
}
精彩评论