我想显示特定来电者的通话记录,该号码来自另一个活动,但查询我尝试不工作请给出解决方案。评论中的代码是我尝试过的。 感谢
public class CallLogs extends Activity {
TextView tv,tv2;
//ListView lv;
//SimpleCursorAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_call_logs);
tv=(TextView)findViewById(R.id.textView1);
tv2=(TextView)findViewById(R.id.textView2);
//lv=(ListView)findViewById(R.id.listView1);
getCallDetails();
String phone = getIntent().getExtras().getString("Phone");
tv2.setText(phone);
}
private void getCallDetails() {
String phone = getIntent().getExtras().getString("Phone");
StringBuffer sb = new StringBuffer();
String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
/* Query the CallLog Content Provider */
Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null,
/*CallLog.Calls.NUMBER+" = ?"*/null,/*new String[]{phone}*/null, strOrder);
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 Log :");
while (managedCursor.moveToNext()) {
String phNum = managedCursor.getString(number);
String callTypeCode = managedCursor.getString(type);
String strcallDate = managedCursor.getString(date);
Date callDate = new Date(Long.valueOf(strcallDate));
String callDuration = managedCursor.getString(duration);
String callType = null;
int callcode = Integer.parseInt(callTypeCode);
switch (callcode) {
case CallLog.Calls.OUTGOING_TYPE:
callType = "Outgoing";
break;
case CallLog.Calls.INCOMING_TYPE:
callType = "Incoming";
break;
case CallLog.Calls.MISSED_TYPE:
callType = "Missed";
break;
}
sb.append("Phone Number: " + phNum + " \nCall Type:"
+ callType + " \nCall Date: " + callDate
+ " \nCall duration in sec : " + callDuration);
sb.append("\n--------------------------");
// adapter = new SimpleCursorAdapter(this,R.layout.custcontview,managedCursor,new String[]{"NUMBER","TYPE","DATE","DURATION"},new int[] {R.id.ccontNo,R.id.ccontName,R.id.ccontType,R.id.textView1});
}
tv.setText(sb);
// lv.setAdapter(adapter);
// managedCursor.close();
}
}
答案 0 :(得分:0)
在清单
中添加以下权限<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.WRITE_CALL_LOG" />
使用以下代码检索日志
public void getLogsByNumber(String[] strNumber ) {
Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, CallLog.Calls.NUMBER + " = ? ", strNumber , "");
if (cursor.moveToFirst ()) {
while (cursor.moveToNext ()) {
String id = cursor.getString (cursor.getColumnIndex (CallLog.Calls._ID));
String number = cursor.getString (cursor.getColumnIndex (CallLog.Calls.NUMBER));
String name = cursor.getString (cursor.getColumnIndex (CallLog.Calls.CACHED_NAME));
}
}
}