利用隐式intent拨打电话

1.在String.xml中添加下列语句

    打电话

2.在fragment_crime.xml中添加打电话按钮

    

3.在 AndroidManifest.xml 中, 标签前面加上申请权限的标签

    

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 +
                ")"
        );
    }
    ....
}
  • 添加数据库字段(CrimeLab.java)
    ....
    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);
            }
            ......
    }
未选择联系人时打电话按钮为不可选状态
选择联系人后打电话按钮变为可选状态
进入拨电话界面后显示联系人手机号

你可能感兴趣的:(利用隐式intent拨打电话)