如何获取带有日期和时间的拨出电话号码?
How to get Outgoing call number with date and time?
我收到来电详细信息 (Number,Name,Date
)。但是如何获取拨出电话的详细信息。我已经为拨出电话的详细信息编写了代码,但它抛出 NullPointerException
。下面是我的 MyCallReceiver
.java 文件和清单文件
public void onReceive(Context context, Intent intent) {
this.context = context;
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Toast.makeText(context, "Call From : " + incomingNumber, Toast.LENGTH_LONG).show();
doToast(getContactName(context, incomingNumber) + " " + incomingNumber);
String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
doToast(currentDateTimeString +" "+incomingNumber);
} else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE) || intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
Toast.makeText(context, "DETECTED CALL HANG UP EVENT", Toast.LENGTH_LONG).show();
String outgoingNumber=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Toast.makeText(context,"Calling To :"+outgoingNumber,Toast.LENGTH_LONG).show();
}
}
首先intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER)给你去电号码,而phone状态是空闲的,然后变成"null",而phone状态变成OFF_HOOK.
最简单的方法是在另一个 onRecive 发生之前保存号码。
为不同的事件(即来电与去电)创建单独的侦听器可能会让您的生活更轻松。对于去电,而不是检查 TelephonyManager 的状态,您可能想要创建一个 IntentFilter
.
IntentFilter filterCalls = new IntentFilter();
filterCalls.addAction(Intent.ACTION_NEW_OUTGOING_CALL);
MyCallReceiver myCallReceiver = new MyCallReceiver();
registerReceiver(myCallReceiver, filterCalls);
然后在您的 onReceive
中您可以简单地拥有:
String outgoingNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER)
并确保它会起作用。
public void onReceive(Context context, Intent intent)
{
String state=intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if(state==null)
{
//Outgoing call
String number=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i("tag","Outgoing number : "+number);
}
else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
{
//Incoming call
String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.i("tag","Incoming number : "+number);
}
}
我收到来电详细信息 (Number,Name,Date
)。但是如何获取拨出电话的详细信息。我已经为拨出电话的详细信息编写了代码,但它抛出 NullPointerException
。下面是我的 MyCallReceiver
.java 文件和清单文件
public void onReceive(Context context, Intent intent) {
this.context = context;
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Toast.makeText(context, "Call From : " + incomingNumber, Toast.LENGTH_LONG).show();
doToast(getContactName(context, incomingNumber) + " " + incomingNumber);
String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
doToast(currentDateTimeString +" "+incomingNumber);
} else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE) || intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
Toast.makeText(context, "DETECTED CALL HANG UP EVENT", Toast.LENGTH_LONG).show();
String outgoingNumber=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Toast.makeText(context,"Calling To :"+outgoingNumber,Toast.LENGTH_LONG).show();
}
}
首先intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER)给你去电号码,而phone状态是空闲的,然后变成"null",而phone状态变成OFF_HOOK.
最简单的方法是在另一个 onRecive 发生之前保存号码。
为不同的事件(即来电与去电)创建单独的侦听器可能会让您的生活更轻松。对于去电,而不是检查 TelephonyManager 的状态,您可能想要创建一个 IntentFilter
.
IntentFilter filterCalls = new IntentFilter();
filterCalls.addAction(Intent.ACTION_NEW_OUTGOING_CALL);
MyCallReceiver myCallReceiver = new MyCallReceiver();
registerReceiver(myCallReceiver, filterCalls);
然后在您的 onReceive
中您可以简单地拥有:
String outgoingNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER)
并确保它会起作用。
public void onReceive(Context context, Intent intent)
{
String state=intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if(state==null)
{
//Outgoing call
String number=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i("tag","Outgoing number : "+number);
}
else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
{
//Incoming call
String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.i("tag","Incoming number : "+number);
}
}