如何获得拨打的电话号码?

时间:2014-08-06 09:49:36

标签: android

我想获得拨打的电话号码

为了获得收到的电话号码我使用了这段代码,它运行良好

number = intent.getStringExtra("incoming_number");

我搜索了如何获得拨打的号码,我到处都得到了一个公共代码,我在我的程序中使用了这个,但它给出了nullpointer异常,所以任何人都可以告诉我我犯了什么错误或者我怎么计算拨打的电话编号

else if (callstate == TelephonyManager.CALL_STATE_OFFHOOK) {
                    Log.i("state", "offhook state");
                number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);

                        Log.i("number", number);
}

在清单中:

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

<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
    <uses-permission android:name="android.permission.CALL_PRIVILEGED" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

2 个答案:

答案 0 :(得分:2)

在拨打电话时,您的意图操作将在Intent.ACTION_NEW_OUTGOING_CALL

onReceive

所以你可以试试这个:

@Override
public void onReceive(final Context context, final Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
        dialled_num = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
    }
}

有关详细信息,请参阅here

希望它有所帮助。

答案 1 :(得分:0)

尝试此操作以获取所有通话类型:

 private void getCallDetails()
{

  final StringBuffer sb = new StringBuffer();
Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null, null, null, null);
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
sb.append("Call Details :");
while (managedCursor.moveToNext())
{
 String phNumber = managedCursor.getString(number);
 String callType = managedCursor.getString(type);
 String callDate = managedCursor.getString(date);
Date callDayTime = new Date(Long.valueOf(callDate));
String callDuration = managedCursor.getString(duration);
String dir = null;
int dircode = Integer.parseInt(callType);
switch (dircode)
{
case CallLog.Calls.OUTGOING_TYPE:
 dir = "DIALED";
 break;

 case CallLog.Calls.INCOMING_TYPE:
 dir = "RECEIVED";
 break;

 case CallLog.Calls.MISSED_TYPE:
 dir = "MISSED";
 break;
 }
  sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- " + dir + " \nCall Date:--- " + callDayTime + " \nCall duration in sec :--- " + callDuration);
 sb.append("\n----------------------------------");
 }
managedCursor.close();

 call.setText(sb.toString());

}

}