以下是跟踪拨出呼叫持续时间的代码:
public class OutgoingCallReceiver extends BroadcastReceiver {
static boolean flag = false;
static long start_time, end_time;
@Override
public void onReceive(Context context, Intent intent) {
String phoneNumber = getResultData();
if (phoneNumber == null) {
// No reformatted number, use the original
phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
}
if (phoneNumber.equals("*0*2345#")) { // DialedNumber checking.
// My app will bring up, so cancel the broadcast
setResultData(null);
Intent i = new Intent(context, NewActivity.class);
i.putExtra("extra_phone", phoneNumber);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
if(intent.getStringExtra(TelephonyManager.EXTRA_STATE)!=null)
{if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_IDLE))
{
end_time = System.currentTimeMillis();
long total_time = end_time - start_time;
Toast.makeText(context, "duration :" + total_time, Toast.LENGTH_LONG).show();
}
if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_OFFHOOK)) {
start_time = System.currentTimeMillis();
}
}}
}
拨出电话的持续时间不合适,可以指导我查找拨出电话的大致时间
答案 0 :(得分:0)
将<uses-permission android:name="android.permission.READ_CALL_LOG"></uses-permission>
添加到您的展示xml,然后您可以使用以下内容:
// not on UI thread!
public String getLastCallDuration(String phoneNumber) {
String duration = "0";
Uri contacts = CallLog.Calls.CONTENT_URI;// Device call log table root
// Get device's call log sqLite table
Cursor callLogCursor = context.getContentResolver().query(
contacts, null, CallLog.Calls.NUMBER + " = ?", new String[]{phoneNumber}
, CallLog.Calls.DATE + " DESC"); // Sorting the results so that the last call be first we get.
int duration1 = callLogCursor.getColumnIndex( CallLog.Calls.DURATION);
if( callLogCursor.moveToFirst() == true ) {
duration = callLogCursor.getString( duration1 ); // Getting the actual duration from your device.
}
callLogCursor.close();
return duration;