我想为联系人群组设置自定义铃声。 我可以为特定联系人设置自定义铃声, 但我不知道如何为联络小组设置一个。
以下是我为自定义联系人设置自定义铃声的代码:
public class RingtoneActivity extends Activity {
Context ctx;
TextView songescape;
static final int PICK_CONTACT = 1, PICK_CONTACTGROUP = 1;
Cursor cursor;
String SDCardRoot;
protected static final int CONTEXTMENU_OPTION1 = 1;
protected static final int CONTEXTMENU_OPTION2 = 2;
protected static final int CONTEXTMENU_OPTION3 = 3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ringtone);
ctx = this;
songescape = (TextView) findViewById(R.id.ringtonetext);
registerForContextMenu(songescape);
}
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
// Set title for the context menu
menu.setHeaderTitle("Set as");
// Add all the menu options
menu.add(1, 1, CONTEXTMENU_OPTION1, "Default Ringtone");
menu.add(1, 2, CONTEXTMENU_OPTION2, "Ringtone to specific Contact");
menu.add(1, 3, CONTEXTMENU_OPTION3, "Ringtone to Contact Group");
}
public boolean onContextItemSelected(MenuItem item) {
// Get extra info about list item that was long-pressed
AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item
.getMenuInfo();
// Perform action according to selected item from context menu
switch (item.getItemId()) {
case CONTEXTMENU_OPTION1:
// set as default ringtone
setRingTone();
break;
case CONTEXTMENU_OPTION2:
// set ringtone to specific contact
Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
/*
* cursor = getContentResolver().query(
* ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
* null, null);
*/
break;
case CONTEXTMENU_OPTION3:
// set ringtone to contacts group
break;
}
return true;
}
public void setRingTone() {
try {
String SDCardRoot =
(Environment.getExternalStorageDirectory() + "/cocktail.mp3");
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, SDCardRoot);
values.put(MediaStore.MediaColumns.TITLE, "COCKTAIL RINGTONE");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
values.put(MediaStore.MediaColumns.SIZE, 2030);
values.put(MediaStore.Audio.Media.ARTIST, "");
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
values.put(MediaStore.Audio.Media.IS_ALARM, true);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(SDCardRoot);
getContentResolver().delete(uri,
MediaStore.MediaColumns.DATA + "=\"" + SDCardRoot + "\"",
null);
Uri newUri = getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(ctx,
RingtoneManager.TYPE_RINGTONE, newUri);
Toast.makeText(getApplicationContext(), "Song is set as Ringtone!",
1000).show();
} catch (Throwable t) {
Toast.makeText(getApplicationContext(), "Exception!", 1000).show();
Log.d("Exception", "catch exception" + t);
}
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String id = c.getString(
c.getColumnIndexOrThrow(
ContactsContract.Contacts._ID));
// set custom ringtone to this id
Uri localUri = Uri.withAppendedPath(
ContactsContract.Contacts.CONTENT_URI, id);
ContentValues localContentValues = new ContentValues();
String hasPhone = c.getString(
c.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone.equalsIgnoreCase("1")) {
Cursor phones = getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + id, null, null);
phones.moveToFirst();
String cNumber = phones.getString(phones
.getColumnIndex("data1"));
System.out.println("number is:" + cNumber);
}
String name = c.getString(
c.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
String SDCardRoot1 = (Environment
.getExternalStorageDirectory() + "/cocktail.mp3");
// from here
localContentValues.put(MediaStore.MediaColumns.DATA,
SDCardRoot);
localContentValues.put(MediaStore.MediaColumns.TITLE,
"COCKTAIL RINGTONE");
localContentValues.put(MediaStore.MediaColumns.MIME_TYPE,
"audio/*");
localContentValues.put(MediaStore.MediaColumns.SIZE, 2030);
localContentValues.put(MediaStore.Audio.Media.ARTIST, "");
localContentValues.put(MediaStore.Audio.Media.IS_RINGTONE,
true);
// localContentValues.put(MediaStore.Audio.Media.IS_NOTIFICATION,
// true);
// localContentValues.put(MediaStore.Audio.Media.IS_ALARM,
// true);
localContentValues.put(MediaStore.Audio.Media.IS_MUSIC,
false);
// to here
localContentValues.put(
ContactsContract.Data.RAW_CONTACT_ID, id);
localContentValues.put(
ContactsContract.Data.CUSTOM_RINGTONE, SDCardRoot1);
getContentResolver().update(localUri, localContentValues,
null, null);
Toast.makeText(this, "Ringtone assigned to: " + name, 0)
.show();
}
}
break;
}
}
}
上下文菜单3是将铃声设置为分组的代码。