Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid - How to set the contact ringtone programmatically?
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/2437922/how-can-i-set-a-ringtone-for-an-individual-contact-on-android">How can I set a ringtone for an individual contact on Android?</a> </p> </blockquote> <p>I am working on sample application on contacts where I can add the contacts in my android app and I can sync the contact at anytime to android native contacts(Address book). I am able to insert the contacts to native contacts now. Only thing is i am not able to set the contact ringtone. Contact ring tone is saved in my DB for all the contacts. But I am struggling to set the ring tone for individual contact. How can i achieve this. Below is the code for saving the contacts. Currently i am hardcoding everything. </p> <pre><code> String DisplayName = "XYZ"; String MobileNumber = "0000000000"; String HomeNumber = "1111"; String WorkNumber = "2222"; String emailID = "email@nomail.com"; String company = "xxxx"; String jobTitle = "abcd"; String Ringtonepath= "content://media/internal/audio/media/43"; Uri Ringtone1 = Uri.parse(Ringtonepath); @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); ArrayList&lt;ContentProviderOperation&gt; ops = new ArrayList&lt;ContentProviderOperation&gt;(); ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI) .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null) .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null) .build() ); //------------------------------------------------------ Names if(DisplayName != null) { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI). withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0). withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE). withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,DisplayName) .build()); } //------------------------------------------------------ Mobile Number if(MobileNumber != null) { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, MobileNumber) .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) .build() ); } //------------------------------------------------------ Home Numbers if(HomeNumber != null) { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, HomeNumber) .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,ContactsContract.CommonDataKinds.Phone.TYPE_HOME) .build()); } //------------------------------------------------------ Work Numbers if(WorkNumber != null) { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, WorkNumber) .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,ContactsContract.CommonDataKinds.Phone.TYPE_WORK) .build()); } //------------------------------------------------------ Email if(emailID != null) { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Email.DATA, emailID) .withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK) .build()); } //------------------------------------------------------ Organization if(!company.equals("") &amp;&amp; !jobTitle.equals("")) { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Organization.COMPANY, company) .withValue(ContactsContract.CommonDataKinds.Organization.TYPE, ContactsContract.CommonDataKinds.Organization.TYPE_WORK) .withValue(ContactsContract.CommonDataKinds.Organization.TITLE, jobTitle) .withValue(ContactsContract.CommonDataKinds.Organization.TYPE, ContactsContract.CommonDataKinds.Organization.TYPE_WORK) .build()); } // Asking the Contact provider to create a new contact try { getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); } catch (Exception e) { e.printStackTrace(); Toast.makeText(getApplicationContext(), "Exception: " + e.getMessage(), Toast.LENGTH_LONG).show(); } } } </code></pre> <p>How can i set the ringtone programmatically. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload