问题是,在小米手机android 8.0上为联系人分配了铃声,但在具有相同android版本的三星手机上却未为其分配铃声。在这两款手机中,都会显示已成功设置铃声的祝酒词,但是当我在小米手机上通话时,铃声已更改,但是当我在三星手机上通话时,铃声是默认铃声。
以下是相关代码:
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.android.vending.BILLING" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
RingtoneActivity.java
当用户点击按钮以设置联系人铃声时,会调用setRingtoneContact。
static final int PICK_CONTACT=1;
public void setRingtoneContact(String pathString){
// contact_selected_ringtone is a global variable
contact_selected_ringtone = pathString;
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
用户选择一个联系人,我们会在onActivityResult中收到结果。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && requestCode == PICK_CONTACT) {
String phoneNo = null;
String name = null;
Uri uri = data.getData();
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
if (cursor.moveToFirst()) {
int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
phoneNo = cursor.getString(phoneIndex);
name = cursor.getString(nameIndex);
Log.e("onActivityResult()", phoneIndex + " " + phoneNo + " " + nameIndex + " " + name);
setForContact(phoneNo);
}
cursor.close();
}
}
此功能实际上是为联系人设置custom_ringtone。
public void setForContact(String contact){
// The Uri used to look up a contact by phone number
final Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, contact);
// The columns used for `Contacts.getLookupUri`
final String[] projection = new String[] {
ContactsContract.Contacts._ID, ContactsContract.Contacts.LOOKUP_KEY
};
final Cursor data = getContentResolver().query(lookupUri, projection, null, null, null);
data.moveToFirst();
try {
// Get the contact lookup Uri
final long contactId = data.getLong(0);
final String lookupKey = data.getString(1);
final Uri contactUri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
if (contactUri == null) {
// Invalid arguments
return;
}
final File file = new File(contact_selected_ringtone);
if (file.exists()){
final String value = Uri.fromFile(file).toString();
final ContentValues values = new ContentValues(1);
values.put(ContactsContract.Contacts.CUSTOM_RINGTONE, value);
try {
int rows = getContentResolver().update(contactUri, values, null, null);
if (rows > 0) {
Toasty.info(getApplicationContext(),getResources().getString(R.string.ringtone_contact_success),Toast.LENGTH_LONG).show();
}
} catch (NullPointerException ex) {
Log.e("contact_exception", ex.toString());
}
}else{
Toast.makeText(this, "file not exist", Toast.LENGTH_SHORT).show();
}
} finally {
data.close();
}
}
我们在这个问题上已经存在了2天了,我们到处都在研究。关于stackoverflow也有一些问题,它们也在做同样的事情。此解决方案有效,但不适用于所有手机。希望找到解决方案。
这是我关于stackoverflow的第一个问题,所以如果我不遵循准则,请指导我。
答案 0 :(得分:0)
好吧,我解决了这个问题。我在这里发布解决方案。
我在 RingtoneActivity.java 中更改了setForContact函数。 这是代码:
public void setForContact(String contact){
// Create File object for the specified ring tone path
// contact_selected_ringtone is a global variable which has the full path to ringtone
File f=new File(contact_selected_ringtone);
// Insert the ring tone to the content provider
ContentValues content_value=new ContentValues();
content_value.put(MediaStore.MediaColumns.DATA, f.getAbsolutePath());
content_value.put(MediaStore.MediaColumns.TITLE, f.getName());
content_value.put(MediaStore.MediaColumns.SIZE, f.length());
content_value.put(MediaStore.MediaColumns.MIME_TYPE,"audio/mp3");
content_value.put(MediaStore.Audio.Media.ARTIST, getString(R.string.app_name));
content_value.put(MediaStore.Audio.Media.IS_ALARM, false);
content_value.put(MediaStore.Audio.Media.IS_MUSIC, false);
content_value.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
content_value.put(MediaStore.Audio.Media.IS_RINGTONE, true);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(f.getAbsolutePath());
getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + f.getAbsolutePath() + "\"", null);
Uri newUri = getContentResolver().insert(uri, content_value);
// The Uri used to look up a contact by phone number
final Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, contact);
final String[] projection = new String[] {
ContactsContract.Contacts._ID, ContactsContract.Contacts.LOOKUP_KEY
};
final Cursor data = getContentResolver().query(lookupUri, projection, null, null, null);
data.moveToFirst();
try {
// Get the contact lookup Uri
final long contactId = data.getLong(0);
final String lookupKey = data.getString(1);
final Uri contactUri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
String str = data.getString(data.getColumnIndexOrThrow("_id"));
Uri localUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, str);
if (contactUri == null) {
// Invalid arguments
return;
}
final File file = new File(contact_selected_ringtone);
final String value = Uri.fromFile(file).toString();
// Apply the custom ringtone_app
final ContentValues values = new ContentValues();
values.put(ContactsContract.Data.RAW_CONTACT_ID, contactId);
values.put(ContactsContract.Data.CUSTOM_RINGTONE, newUri.toString());
try {
int rows = getContentResolver().update(localUri, values, null, null);
if (rows > 0) {
Toasty.info(getApplicationContext(),getResources().getString(R.string.ringtone_contact_success),Toast.LENGTH_LONG).show();
}
} catch (NullPointerException ex) {
Log.e("contact_exception", ex.toString());
}
} finally {
data.close();
}
}
希望它对其他人有帮助。