我该怎么做?我想添加我的通话记录应用程序,时间(开始和结束),如果照片和名称已经有电话联系,我想在我的应用程序中添加这张照片和名字。如果不是我想设置默认照片。我想显示电话号码。
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Locale;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView PhoneNumber = null;
TextView Date = null;
// TextView StartEnd = null;
TextView Duration = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PhoneNumber = (TextView) findViewById(R.id.txtPhoneNumber);
Date = (TextView) findViewById(R.id.txtDate);
// StartEndTime = (TextView)findViewById(R.id.txtStartEnd);
Duration = (TextView) findViewById(R.id.txtDuration);
getCallDetails();
}
private void getCallDetails() {
StringBuffer sbPhoneNumber = new StringBuffer();
StringBuffer sbDate = new StringBuffer();
// StringBuffer sbStart = new StringBuffer();
StringBuffer sbDuration = 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);
while (managedCursor.moveToNext()) {
String phNum = managedCursor.getString(number);
String callTypeCode = managedCursor.getString(type);
String strcallDate = managedCursor.getString(date);
Date callDayTime = new Date(Long.valueOf(strcallDate));
String dateStr = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH).format(callDayTime);
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;
}
sbPhoneNumber.append(phNum + " (" + callType + ") ");
sbDate.append(dateStr);
sbDuration.append(callDuration);
}
managedCursor.close();
PhoneNumber.setText(sbPhoneNumber);
Date.setText(sbDate);
Duration.setText(sbDuration);
}
}
答案 0 :(得分:1)
你已经完成了呼叫记录部分,我认为你可以在哪里获得持续时间等。 从通话记录中取出号码后,您必须使用联系人获取联系方式。
参考
http://www.androidhub4you.com/2013/06/get-phone-contacts-details-in-android_6.html
答案 1 :(得分:0)
第1步:目前您使用呼叫记录API **来检索日志历史记录,我建议您使用** ContactsContract API 来检索相关的电话号码和该号码的照片,如图所示下方。
让我们从联系人中检索照片,我们可以编写一个方法
private void retrieveContactPhoto() {
Bitmap photo = null;
try {
// contacts API
InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(),
ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(contactID)));
if (inputStream != null) {
photo = BitmapFactory.decodeStream(inputStream);
ImageView imageView = (ImageView) findViewById(R.id.img_contact);
imageView.setImageBitmap(photo);
}
assert inputStream != null;
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
步骤2:类似于联系号码
private void retrieveContactNumber() {
String contactNumber = null;
// getting contacts ID
Cursor cursorID = getContentResolver().query(uriContact,
new String[]{ContactsContract.Contacts._ID},
null, null, null);
if (cursorID.moveToFirst()) {
contactID = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts._ID));
}
cursorID.close();
Log.d(TAG, "Contact ID: " + contactID);
// Using the contact ID now we will get contact phone number
Cursor cursorPhone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,
new String[]{contactID},
null);
if (cursorPhone.moveToFirst()) {
contactNumber = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
cursorPhone.close();
Log.d(TAG, "Contact Phone Number: " + contactNumber);
}
步骤3:重新联系姓名
private void retrieveContactName() {
String contactName = null;
// querying contact data store
Cursor cursor = getContentResolver().query(uriContact, null, null, null, null);
if (cursor.moveToFirst()) {
// DISPLAY_NAME = The display name for the contact.
// HAS_PHONE_NUMBER = An indicator of whether this contact has at least one phone number.
contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
}
cursor.close();
Log.d(TAG, "Contact Name: " + contactName);
}
步骤4:要使用联系人,您必须在Android Manifest文件中设置权限,如下所示。
<uses-permission android:name="android.permission.READ_CONTACTS" />