How to add ringtone to native contacts of application programmatically in android

I know how to create new contacts in android, you can create a contact with a name, mobile number, email id for your own application. But I do not know how to create contact with the ringtone. Please help me. thanks in advance

I got a decision to add a ringtone after adding contacts to my native application:

String select = "(" + ContactsContract.Contacts.DISPLAY_NAME + " == \"" +first_name+ "\" )"; Cursor c1 = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, select, null, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"); startManagingCursor(c1); int id=0; if (c1.moveToNext()) { id = new Integer(c1.getString(0)).intValue(); Toast.makeText(getApplicationContext(), "CONTACT ID: "+id+"", Toast.LENGTH_LONG).show(); } ContentResolver cr = getContentResolver(); cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); ContentValues values=new ContentValues(); values.put(ContactsContract.Contacts.CUSTOM_RINGTONE, newgroup_ringtone); cr.update(ContactsContract.Contacts.CONTENT_URI, values, Contacts._ID + "=" + id, null); 
+4
source share
1 answer

Why not just add a contact first, and then restore that contact and update the contact using the ringtone using the code as follows:

 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); ops.add(ContentProviderOperation.newUpdate(ContactsContract.Contacts.CONTENT_URI) .withSelection(ContactsContract.Contacts._ID + " = ?", new String[] {id}) .withValue(ContactsContract.Contacts.STARRED, starred) .withValue(ContactsContract.Contacts.SEND_TO_VOICEMAIL, sendToVoicemail) .withValue(ContactsContract.Contacts.CUSTOM_RINGTONE, ringtone) .build()); try { resolver.applyBatch(ContactsContract.AUTHORITY, ops); } 
+1
source

Source: https://habr.com/ru/post/1435915/


All Articles