Yêu cầu Jun 16th, 2019 9:08 a.m. 104 0 0
  • 104 0 0
+1

[Android] How to set custom ringtone for a specific contact

Chia sẻ
  • 104 0 0

Chào mọi người mình muốn hỏi về các set custom ringtone cho 1 số liên lạc cụ thể, mình đã biết việc lấy được thông tin về số liên lạc(ID, Tên, Số điện thoại,...) cũng như đã biết cách set nhạc chuông cho máy nhưng làm sao để thay đổi ringtone cho 1 số liên lạc cụ thể.
Dưới đây là cách mình làm nhưng nó không thành công:

Mình đã thử lợi dụng thằng intent(RingtoneManager.ACTION_RINGTONE_PICKER) để tới được activity chọn nhạc chuông mặc định của hệ thống:

ic_ringtone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    if (Settings.System.canWrite(ContactDetailActivity.this)) {
                        Intent intent=new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
                        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, ringtone);
                        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, ringtone);
                        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI, ringtone);
                        startActivityForResult(intent , 24);
                    }else {
                        Toast.makeText(ContactDetailActivity.this, "Vui lòng cấp quyền để đặt nhạc chuông"
                                , Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    }
                }
            }
        });

Còn đây là hàm set ringtone của mình với tham số path là đường dẫn tới ringtone sau khi mình chọn xong:

public void addRingtone(String path){
//Phone is phone number of contact that I got
           final Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, phone);
           final String []projection = new String[] {ContactsContract.Contacts._ID
                   , ContactsContract.Contacts.LOOKUP_KEY};
           final Cursor data = getContentResolver().query(lookupUri, projection, null, null, null);
           data.moveToFirst();
           try {
               // Get the contact lookup Uri
               final long contactId = data.getLong(0);
               final String lookupKey = data.getString(1);
               final Uri contactUri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
               if (contactUri == null) {
                   // Invalid arguments
                   return;
               }

               // Apply the custom ringtone
               final ContentValues values = new ContentValues(1);
               values.put(ContactsContract.Contacts.CUSTOM_RINGTONE, path);
               getContentResolver().update(contactUri, values, null, null);

           } finally {
               // Don't forget to close your Cursor
               data.close();
           }
       }

Cuối cùng là hàm onActivityResult để đón thằng path và sau đó thực thi hàm set ringtone ở trên:

@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 24:
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        if (Settings.System.canWrite(ContactDetailActivity.this)) {
                            ringtone = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
                            addRingtone(ringtone.getPath());
//                            Toast.makeText(ContactDetailActivity.this, ringtone.getPath() + " " + phone + " " + id, Toast.LENGTH_SHORT).show();
                        }else {
                            Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                        }
                    }

                    break;

                default:
                    break;
            }
        }
    }

@@ mình mất mấy ngày cho việc này r mà vẫn chưa giải quyết được. Ai có cách nào hoặc đã từng làm phần này cho mình giải pháp được không ?. Mình xin cảm ơn!

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí