这二天,在联系人(Contacts)的新建编辑界面添加了一个来电手机铃声的功能,现在把一些关键的地方写出来,做一个小笔记吧。
1. 功能需求:
在新建编辑联系人界面,添加一个手机铃声入口,点击可以进入铃声选择界面,返回一个手机铃声的url.
2. 一些说明:
这主要涉及到的db数据库:
data/data/com.android.providers.contacts/databases/contacts2.db 表:contacts url: android.provider.ContactsContract.Contacts.CONTENT_URI 我们到时候,主要要用到的,还是lookup和custom_ringtone这二个字段。3. 关键的逻辑处理代码:
(1)在RawContactEditorView.java文件中,添加手机铃声界面,当点击来电铃声时,发出选择来电铃声的Intent
- private void doPickRingtone() {
- Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
- intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
- intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE);
- intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
- Uri ringtoneUri;
- /**
- if (mCustomRingtone != null) {
- ringtoneUri = Uri.parse(mCustomRingtone);
- } else {
- ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
- }
- **/
- ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
- intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, ringtoneUri);
- ((Activity)mContext).startActivityForResult(intent, REQUEST_CODE_PICK_RINGTONE);
- android.util.Log.i("set_ring_tone","((Activity)mContext):"+((Activity)mContext));
- }
(2).在ContactEditorActivity 中得到返回的铃声uri:
- //add by wingtech hexiaoming for add_ring_tone 20141107 start
- public static Uri pickedUri = null;
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- if (resultCode != Activity.RESULT_OK) {
- return;
- }
- switch (requestCode) {
- case RawContactEditorView.REQUEST_CODE_PICK_RINGTONE: {
- pickedUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
- //handleRingtonePicked(pickedUri);
- android.util.Log.i("set_ring_tone","pickedUri:"+pickedUri);
- String ringtone_title = getRingtoneName(pickedUri);
- updateRingtone(ringtone_title);
- break;
- }
- }
- }
- //add by wingtech hexiaoming for add_ring_tone 20141107 end
(3).用铃声的uri值得到铃声的名称:
- public String getRingtoneName(Uri ringtone) {
- String ringtone_title = null;
- try {
- Cursor cursor = getContentResolver().query(ringtone,
- new String[] { MediaStore.Audio.Media.TITLE }, null, null, null);
- if (cursor != null) {
- if (cursor.moveToFirst()) {
- ringtone_title = cursor.getString(0);
- }
- cursor.close();
- }
- } catch (SQLiteException sqle) {
- // Unknown title for the ringtone
- sqle.printStackTrace();
- }
- return ringtone_title;
- }
(4)更新来电铃声的名称:
- private void updateRingtone(String ringtone_title) {
- mFragment.updateRingtone(ringtone_title);
- }
(5)在ContactEditorFragment.java 我们保存来电铃声的uri,这个其实主要是靠mLookupUri 来做条件,来对应的保存:
- private void handleRingtonePicked(Uri pickedUri) {
- if (pickedUri == null || RingtoneManager.isDefault(pickedUri)) {
- mCustomRingtone = null;
- } else {
- mCustomRingtone = pickedUri.toString();
- }
- Intent intent = ContactSaveService.createSetRingtone(
- mContext, mLookupUri, mCustomRingtone);
- mContext.startService(intent);
- }
我们在ContactEditorFragment----save方法的,调用handleRingtonePicked此方法,就OK。 但是,这有一个在新建时,mLookupUri 为null的情况,所以,我们还是要在ContactSaveService -----saveContact 方法中,找到lookupUri的不为null值的地方加上:
- //add by wingtech hexiaoming for add_ring_tone 20141107 start
- android.util.Log.i("saveContact","lookupUri_3:"+lookupUri);
- if(ContactEditorActivity.pickedUri != null){
- Intent intent_save_ringtone = createSetRingtone(this, lookupUri, ContactEditorActivity.pickedUri.toString());
- startService(intent_save_ringtone);
- }
- //add by wingtech hexiaoming for add_ring_tone 20141107 end
(6)当我们刚进来时,我们就要读取根据lookup的值来读取custom_ringtone,然后,再根据custom_ringtone读取(getRingtoneName)铃声的名称,显示出来。
- public String getRingtoneFromLookup(String lookup) {
- Cursor cursor = mContext.getContentResolver().query(
- android.provider.ContactsContract.Contacts.CONTENT_URI,
- new String[]{android.provider.ContactsContract.Contacts.CUSTOM_RINGTONE},
- android.provider.ContactsContract.Contacts.LOOKUP_KEY + "= ?",
- new String[]{lookup},
- null);
- try {
- if (cursor.moveToFirst()) {
- android.util.Log.i("ContactEditorFragment","cursor.getString(0):"+cursor.getString(0));
- return cursor.getString(0);
- }
- } finally {
- cursor.close();
- }
- return null;
- }