1.在String.xml中添加下列语句
<string name="call">打电话</string>
2.在fragment_crime.xml中添加打电话按钮
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/crime_call"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:enabled="false"
android:text="@string/call"/>
3.在 AndroidManifest.xml 中, 标签前面加上申请权限的标签
<uses-permission android:name="android.permission.READ_CONTACTS"/>
4.添加手机号码信息至模型层
- 添加mPhoneNumber成员变量(Crime.java)
public class Crime {
private String mPhoneNumber;
....
public String getPhoneNumber(){
return mPhoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
mPhoneNumber = phoneNumber;
}
}
- 添加数据库字段(CrimeDbSchema.java)
public class CrimeDbSchema {
public static final class CrimeTable{
....
public static final class Cols{
....
public static final String PHONE_NUMBER ="phone_number";
}
}
}
- 添加数据库字段(CrimeBaseHelper.java)
public class CrimeBaseHelper extends SQLiteOpenHelper{
....
@Override
public void onCreate(SQLiteDatabase db){
....
CrimeTable.Cols.SUSPECT+","+
CrimeTable.Cols.PHONE_NUMBER +
")"
);
}
....
}
....
private static ContentValues getContentValues(Crime crime){
....
values.put(CrimeTable.Cols.PHONE_NUMBER, crime.getPhoneNumber());
return values;
}
....
- 读取手机号码信息(CrimeCursorWrapper.java)
....
public Crime getCrime(){
....
String phoneNumber=getString(getColumnIndex(CrimeTable.Cols.PHONE_NUMBER));
....
crime.setPhoneNumber(phoneNumber);
return crime;
}
}
5.新增getPhoneNumberById()方法(CrimeFragment.java)
....
private String getCrimeReport(){
....
}
private String getPhoneNumberById(String contactId) {
Uri phoneUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
Cursor c = getActivity().getContentResolver().query(
phoneUri,
new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER },
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "= ? ",
new String[] { contactId },
null
);
String phoneNumber = null;
try {
if (c.getCount() == 0) {
return phoneNumber;
}
c.moveToFirst();
phoneNumber = c.getString(0);
} finally {
c.close();
}
return phoneNumber;
}
6.建立拨打电话的隐式 Intent(CrimeFragment.java)
....
private Button mCallButton;
....
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){
....
mCallButton=(Button)v.findViewById(R.id.crime_call);
if (mCrime.getSuspect() != null) {
handleCallSuspectButton(mCrime.getPhoneNumber());
} else {
mCallButton.setText(getString(R.string.call));
mCallButton.setEnabled(false);
}
mCallButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Uri number = Uri.parse("tel:"+ mCrime.getPhoneNumber());
Intent i = new Intent(Intent.ACTION_DIAL, number);
startActivity(i);
}
});
....
}
7.修改CrimeFragment.java
....
public void onActivityResult(int requestCode, int resultCode, Intent data){
....
}else if (requestCode==REQUEST_CONTACT&&data!=null){
Uri contactUri=data.getData();
String[] queryFields=new String[]{
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts._ID
};
Cursor c=getActivity().getContentResolver().query(contactUri,queryFields,null,null,null);
try {
......
String suspect=c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String suspectId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
String phoneNumber = getPhoneNumberById(suspectId);
mCrime.setSuspect(suspect);
mCrime.setPhoneNumber(phoneNumber);
updateCrime();
mSuspectButton.setText(suspect);
handleCallSuspectButton(phoneNumber);
}
......
}