我想开发Android应用程序,我需要分配铃声到特定的联系电话,而不允许访问联系人列表的用户。
这里是code分配铃声给所有联系人:
文件k =新的文件(/ SD卡/ AudioRecorder,hello.mp4);
//乌里I = data.getData(); // GETDATA
//的String = i.getPath(); // getPath
//文件k =新的文件(S); //从路径设置文件
//如果(S!= NULL){//(file.exists
ContentValues值=新ContentValues();
values.put(MediaStore.MediaColumns.DATA,k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE,环);
values.put(MediaStore.MediaColumns.MIME_TYPE,音频/ MP4);
values.put(MediaStore.MediaColumns.SIZE,k.length());
values.put(MediaStore.Audio.Media.ARTIST,R.string.app_name);
values.put(MediaStore.Audio.Media.IS_RINGTONE,真正的);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION,真正的);
values.put(MediaStore.Audio.Media.IS_ALARM,真正的);
values.put(MediaStore.Audio.Media.IS_MUSIC,假);
开放的我们的uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
。getContentResolver()删除(URI,MediaStore.MediaColumns.DATA += \+ k.getAbsolutePath()+\,NULL);
乌里newUri = getContentResolver()插入(URI,值)。
尝试 {
RingtoneManager.setActualDefaultRingtoneUri(MainActivity.this,RingtoneManager.TYPE_RINGTONE,newUri);
}赶上(的Throwable T){
}
我怎么能做到这一点的具体联系?
解决方案设置自定义铃声到特定联系人的号码
Android有这一个专栏: ContactsContract.CUSTOM_RINGTONE
。
所以,你可以使用 ContactsContract.Contacts.getLookupUri
让你的联系人的乌里
,即$后p $ ptty的多少,所有剩下的是调用 ContentResolver.update
。
下面是查找联系人通过自己的手机号码,然后应用自定义铃声的例子:
进口android.provider.ContactsContract.Contacts;
进口android.provider.ContactsContract.PhoneLookup;
//的URI用于查找联系人电话号码
最后乌里lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,012-345-6789);
//使用`Contacts.getLookupUri`列
最终的String []投影=新的String [] {
Contacts._ID,Contacts.LOOKUP_KEY
};
//建立你的光标
最后光标数据= getContentResolver()查询(lookupUri,投影,NULL,NULL,NULL);
data.moveToFirst();
尝试 {
//获取联系人查找乌里
最终长的ContactID = data.getLong(0);
最后弦乐lookupKey = data.getString(1);
最后乌里contactUri = Contacts.getLookupUri(的ContactID,lookupKey);
如果(contactUri == NULL){
//参数无效
返回;
}
//获取铃声的你想使用的路径
最终字符串存储= Environment.getExternalStorageDirectory()getPath()。
最终文件的文件=新的文件(存储+/ AudioRecorder,hello.mp4);
最终的字符串值= Uri.fromFile(文件)的ToString();
//应用自定义铃声
最终ContentValues值=新ContentValues(1);
values.put(Contacts.CUSTOM_RINGTONE,价值);
getContentResolver()更新(contactUri,价值观,NULL,NULL);
} 最后 {
//不要忘记关闭游标
data.close();
}
此外,你需要同时添加的权限读取和写入联系人:
<使用-权限的Android:名称=android.permission.READ_CONTACTS/>
<使用-权限的Android:名称=android.permission.WRITE_CONTACTS/>
I am trying to develop android app, I need to assign ringtone to specific contact number without permit the user to access list of contact.
here is the code for assigning ringtone to all contact :
File k = new File("/sdcard/AudioRecorder", "hello.mp4");
// Uri i = data.getData(); //getDATA
//String s = i.getPath(); //getPath
// File k = new File(s); //set File from path
//if(s!=null){ //(file.exists
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "ring");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp4");
values.put(MediaStore.MediaColumns.SIZE, k.length());
values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
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(k.getAbsolutePath());
getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);
Uri newUri = getContentResolver().insert(uri, values);
try {
RingtoneManager.setActualDefaultRingtoneUri(MainActivity.this, RingtoneManager.TYPE_RINGTONE, newUri);
} catch (Throwable t) {
}
How can I do this for specific contact?
解决方案set custom ringtone to specific contact number
Android has a special column for this: ContactsContract.CUSTOM_RINGTONE
.
So, you could use ContactsContract.Contacts.getLookupUri
to get your contact's Uri
, after that pretty much all that's left is to call ContentResolver.update
.
Here's an example of looking up a contact by their phone number, then applying a custom ringtone:
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.PhoneLookup;
// The Uri used to look up a contact by phone number
final Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, "012-345-6789");
// The columns used for `Contacts.getLookupUri`
final String[] projection = new String[] {
Contacts._ID, Contacts.LOOKUP_KEY
};
// Build your Cursor
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 = Contacts.getLookupUri(contactId, lookupKey);
if (contactUri == null) {
// Invalid arguments
return;
}
// Get the path of ringtone you'd like to use
final String storage = Environment.getExternalStorageDirectory().getPath();
final File file = new File(storage + "/AudioRecorder", "hello.mp4");
final String value = Uri.fromFile(file).toString();
// Apply the custom ringtone
final ContentValues values = new ContentValues(1);
values.put(Contacts.CUSTOM_RINGTONE, value);
getContentResolver().update(contactUri, values, null, null);
} finally {
// Don't forget to close your Cursor
data.close();
}
Also, you'll need to add both permissions to read and write contacts:
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />