简体   繁体   English

未在Android的Cordova中设置“联系人铃声”

[英]Contact Ringtone not setting in Cordova for Android

I'm looking to create a plugin for setting ringtones to contacts using Cordova / Android, but I haven't had any luck after messing around with the following code for a while: 我正在寻找创建一个插件,以使用Cordova / Android为联系人设置铃声,但是一段时间后,我没有任何运气:

package com.mypackage.contactringtone;

import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.PhoneLookup;
import android.provider.ContactsContract.CommonDataKinds;
import android.provider.ContactsContract.CommonDataKinds.*;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.LOG;
import org.json.JSONArray;
import org.json.JSONException;
import java.lang.Object;
import android.net.Uri;
import java.io.File;
import android.content.Context;
import android.content.ContentValues;
import android.database.Cursor;
import android.provider.MediaStore;

public class ContactRingtone extends CordovaPlugin {

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        if ("set_ringtone".equals(action)) {
            String contact_id = args.getString(0);
            String phone_number = args.getString(1);
            String ringtone_path = args.getString(2);

            int my_contact_id = Integer.parseInt(contact_id);

        Context context = cordova.getActivity();
            Uri contact_data = ContactsContract.Contacts.CONTENT_URI;
            File ringtone_file = new File(ringtone_path);

            String[] projection = new String[] {
                ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Contacts.HAS_PHONE_NUMBER
            };

            String selection = ContactsContract.CommonDataKinds.Phone._ID + "='" + my_contact_id + "'";

            Cursor local_cursor = context.getContentResolver().query(contact_data, projection, selection, null, null);
            local_cursor.move(1);

            String this_id = local_cursor.getString(local_cursor.getColumnIndexOrThrow("_id"));
            Uri local_uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, this_id);
            ContentValues local_content_values = new ContentValues();

            local_content_values.put(ContactsContract.Data.RAW_CONTACT_ID, my_contact_id);
            local_content_values.put(ContactsContract.Data.CUSTOM_RINGTONE, ringtone_file.getAbsolutePath() + "/" + contact_id + ".ogg");
            context.getContentResolver().update(local_uri, local_content_values, null, null);

            callbackContext.success("Ringtone set for: " + my_contact_id + " - " + phone_number + " - " + ringtone_path);
            return true;
        }

        callbackContext.error("Invalid action: " + action);
        return false;
    }
}

Basically, I'm using the following plugins to get contact data and ringtone path: 基本上,我正在使用以下插件来获取联系人数据和铃声路径:

cordova-plugin-contacts cordova-plugin-RingtoneSelector cordova-plugin-contacts cordova-plugin-RingtoneSelector

Once I get the contact ID and ringtone path, I send them to my custom plugin as arguments. 获得联系人ID和铃声路径后,我会将其作为参数发送到我的自定义插件。 Contact ID example is "1". 联系人ID示例为“ 1”。 Ringtone path example is "content://media/internal/audio/media/34". 铃声路径示例为“ content:// media / internal / audio / media / 34”。

I am not seeing any errors returned and the success callback returns the data from the Java function correctly, so I'm assuming I'm just not querying something correctly. 我没有看到返回的任何错误,并且成功回调正确地从Java函数返回了数据,因此我假设我只是在查询不正确的东西。

I'm fairly new to Java, so this code is becoming a mess. 我对Java还是很陌生,所以这段代码变得一团糟。 What needs to be updated in the above code to be able to set a ringtone to a contact properly? 上面的代码中需要更新哪些内容才能正确设置联系人的铃声? Thanks in advance. 提前致谢。

Few issues I find here: 我在这里发现的几个问题:

  1. contact-id is a Long not an Integer on Android, so Integer.parseInt(contact_id) is wrong. contact-id是Long而不是Android上的Integer,因此Integer.parseInt(contact_id)是错误的。
  2. CommonDataKinds.Phone._ID is not the contact-id, it's the id of the Data row of a phone number, so selection is wrong. CommonDataKinds.Phone._ID不是联系人ID,而是电话号码的数据行的ID,因此selection错误。
  3. new File(ringtone_path) make sure you have permission to access this path, especially if you're using Runtime-permissions on Marshmallow+ devices. new File(ringtone_path)确保您具有访问此路径的权限,特别是如果您在Marshmallow +设备上使用运行时权限。
  4. Data.RAW_CONTACT_ID is the ID of a RawContact , not Contact . Data.RAW_CONTACT_ID是一个的ID RawContact ,没有Contact

Here's the part of the Contacts update code that should work: 这是应该起作用的“联系人”更新代码部分:

Long myContactId = Long.parseLong(contact_id);
final String ringtoneUri = Uri.fromFile(ringtone_file).toString();

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Contacts.CONTENT_URI)
        .withSelection(ContactsContract.Contacts._ID + " = '" + myContactId + "'")
        .withValue(ContactsContract.Contacts.CUSTOM_RINGTONE, ringtoneUri)
        .build());

try {
    resolver.applyBatch(ContactsContract.AUTHORITY, ops);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM