2015-02-07 58 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

相關問題