Android - 如何以编程方式设置联系人铃声?

时间:2012-09-25 13:09:47

标签: android sqlite contacts ringtone

  

可能重复:
  How can I set a ringtone for an individual contact on Android?

我正在处理联系人的示例应用程序,我可以在我的Android应用程序中添加联系人,我可以随时将联系人同步到Android本机联系人(地址簿)。我现在可以将联系人插入到本机联系人。唯一的问题是我无法设置联系人铃声。所有联系人都会在我的数据库中保存联系人铃声。但我正在努力为个人联系设置铃声。我怎样才能做到这一点。以下是保存联系人的代码。目前我正在硬编码一切。

    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<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();


        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("") && !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();

        } 




    }
}

如何以编程方式设置铃声。

0 个答案:

没有答案