2015-02-07 59 views
0

如果我试图将位于我的SD卡中的铃声指定给特定的联系人。它的工作完美,但问题是,该铃声分配给所有的联系人,而不是特定的联系人。以编程方式指定在Android系统中联系手机铃声

这里是我的代码: -

((ListView) v.findViewById(R.id.contact_list)).setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
       ContactModel contactModel = (ContactModel) adapterView.getItemAtPosition(i); 

       AudioModel audioModel = (AudioModel) getArguments().getSerializable("AudioModel"); 

       ContentValues values = new ContentValues(); 
       String username = contactModel.getName(); 

       Log.e("SYNC", "setting ringtone for " + username); 
       ContentResolver resolver = getActivity().getContentResolver(); 

       File root = Environment.getExternalStorageDirectory(); 

       Log.e("test", "ringtone checkpoint name here: beautiful "); 

       File file = new File(audioModel.getPath()); 
       if(file.exists()) { 

        Log.e("test", "ringtone checkpoint if file exists"); 

        Uri oldUri = MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath()); 
        resolver.delete(oldUri, MediaStore.MediaColumns.DATA + "=\"" + file.getAbsolutePath() + "\"", null); 

        Uri contactUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, contactModel.getContact_id()); 

        values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath()); 
        values.put(MediaStore.MediaColumns.TITLE, "Beautiful"); 
        values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3"); 
        values.put(MediaStore.Audio.Media.IS_RINGTONE, true); 

        Uri uri = MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath()); 
        Uri newUri = resolver.insert(uri, values); 
        String uriString = newUri.toString(); 
        values.put(ContactsContract.Contacts.CUSTOM_RINGTONE, uriString); 
        Log.e("Uri String for " + ContactsContract.Contacts.CONTENT_URI, uriString); 
        long updated = resolver.update(contactUri, values,null, null); 
        Toast.makeText(getActivity(), "Updated : " + updated, Toast.LENGTH_LONG).show(); 
       } else { 
        Toast.makeText(getActivity(), "File does not exist", Toast.LENGTH_LONG).show(); 
       } 

      } 
     }); 

如果我使用这个代码,它的工作,但铃声分配给所有联系人没有一个。

resolver.update(ContactsContract.Contacts.CONTENT_URI, values,null, null); 

但是,当我使用此代码,它不工作。

Uri contactUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, contactModel.getContact_id()); 
resolver.update(contactUri, values,null, null); 

回答

0

您的更新没有任何where子句,因此它将更新所有联系人。这就是两个最后的参数 - where子句和它的一组值。

+0

你能告诉我该怎么把在WHERE子句 – asdf 2015-02-07 10:40:04

0

根据这个问题,我实现了如何设置铃声的接触和我张贴的答案here

相关问题