http://www.eoeandroid.com/thread-263749-1-1.html
好久没有发帖了,这周做了个将音乐设置为来电铃声,短信铃声,联系人铃声这些小功能!额,具体说说方法吧
//add by Aaron
case R.id.ring_tone://来电铃声
FileInfo fileInfo = mAdapter.getFirstCheckedFileInfoItem();
if (fileInfo != null) {
String tone_path = fileInfo.getFileAbsolutePath();
Utils.setMyRing(FileManagerOperationActivity.this, tone_path);
}
break;
//add by Aaron
case R.id.ring_message://短信铃声
FileInfo fileInfos = mAdapter.getFirstCheckedFileInfoItem();
if (fileInfos != null) {
String message_path =fileInfos.getFileAbsolutePath();
Utils.setMyMessage(FileManagerOperationActivity.this, message_path);
};
break;
//add by Aaron
case R.id.ring_contact://联系人铃声
Intent intent = new Intent("android.intent.action.contacts.list.PICKMULTICONTACTS");
intent.setType(Contacts.CONTENT_TYPE);
startActivityForResult(intent, REQUEST_CODE_TEXT_VCARD);
break;
default:
return false;
}
return true;
}
//add by Aaron
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CODE_TEXT_VCARD:
if (data != null) {
long[] contactIds = data.getLongArrayExtra("com.mediatek.contacts.list.pickcontactsresult");
FileInfo fileInfo = mAdapter.getFirstCheckedFileInfoItem();
String path = fileInfo.getFileAbsolutePath();
for(int i = 0; i< contactIds.length; i++)
{
//Uri contactUri = Uri.parse("content://com.android.contacts/raw_contacts/"+contactIds );
Utils.setMyContact(FileManagerOperationActivity.this, path, contactIds);
}
} else {
}
break;
default:
break;
}
}//这段代码是加在文件管理夹apk下面的,就是三个menu选项,分辨对应设置来电铃声,短信铃声以及联系人铃声
/* Copyright Statement:
*
*add by Aaron
*/
package com.mediatek.filemanager;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import java.io.File;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.media.RingtoneManager;
import android.net.Uri;
import android.content.Intent;
import android.provider.MediaStore;
import android.provider.Settings;
import android.widget.Toast;
public class Utils {
//设置为短信铃声
static void setMyMessage(Context context, String path) {
File sdfile = new File(path);
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, sdfile.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, sdfile.getName());
values.put(MediaStore.MediaColumns.SIZE, sdfile.length());
// values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(path);
Uri newUri = context.getContentResolver().insert(uri, values);
Intent i = new Intent("com.android.mms.change.ring");
i.putExtra("newUri", newUri.toString());
context.sendBroadcast(i);//发一个广播到短信模块,让短信模块自己修改自己的SharedPreferences
Toast.makeText(context, "setMessage-----"+sdfile.getName()+"---------------->",
Toast.LENGTH_SHORT).show();
}
//设置为联系人铃声
static void setMyContact(Context context, String path, long id){
File sdfile = new File(path);
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, sdfile.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, sdfile.getName());
values.put(MediaStore.MediaColumns.SIZE, sdfile.length());
// values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(path);
System.out.println("uri----------------------->"+uri.toString());
Uri newUri = context.getContentResolver().insert(uri, values);
System.out.println("newUri----------------------->"+newUri.toString());
ContentValues contactvalues = new ContentValues();
contactvalues.put(Contacts.CUSTOM_RINGTONE, newUri.toString());
context.getContentResolver().update(Contacts.CONTENT_URI,
contactvalues, Contacts._ID + " = " + String.valueOf(id), null);
Toast.makeText(context, "Contact-------"+sdfile.getName()+"-------------->",
Toast.LENGTH_SHORT).show();
}
//设置为来电铃声
static void setMyRing(Context context, String path) {
File filePath = new File(path);
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, filePath.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, filePath.getName());
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(filePath
.getAbsolutePath());
context.getContentResolver().delete(
uri,
MediaStore.MediaColumns.DATA + "=\""
+ filePath.getAbsolutePath() + "\"", null);
Uri newUri = context.getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(context,
RingtoneManager.TYPE_RINGTONE, newUri);
Toast.makeText(context, "宸插皢"+filePath.getName()+"璁句负鎵嬫満閾冨0", Toast.LENGTH_SHORT).show();
}
}//这是一个类,注意了:设置短信铃声并非设置通知铃声哦.....看下图就明白了
好吧,破网络,图片传不上,用adb进入/data/data/com.android.mms/shared_prefs,然后cat com.android.mms_preferences.xml,也就是打开这个XML文件,你会看到类似如下东西
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="pref_key_ringtone">content://media/external/audio/media/196</string>//注意这行就是代表短信铃声的URI了
<string name="pref_key_mms_creation_mode">FREE</string>
<boolean name="pref_key_enable_notifications" value="true" />
<string name="pref_key_sms_save_location">Phone</string>
<string name="pref_key_mms_size_limit">300</string>
<string name="pref_key_sms_input_mode">Automatic</string>
<boolean name="CmccMmsUiMode" value="false" />
<string name="pref_key_mms_priority">Normal</string>
<boolean name="checked_message_limits" value="true" />
<boolean name="pref_key_mms_auto_retrieval" value="true" />
<boolean name="pref_key_wappush_enable" value="true" />
<int name="CmccSimCardInfo" value="0" />
也就是你只要修改了<string name="pref_key_ringtone">content://media/external/audio/media/196</string>就修改成功了,具体看上面的代码
设置联系人铃声就不说了,调用联系人apk,得到id(是long型的),然后修改数据库,搞定,上面代码应该是有小bug的,发现的记得回复哈! 注:本人已经测试成功了的(所以不用怀疑)