【问题标题】:Android set Custom ringtone for user defined numberAndroid为用户自定义号码设置自定义铃声
【发布时间】:2021-02-25 12:13:49
【问题描述】:

我正在编写用于在 android 中设置自定义铃声的代码。从stackoverflow中的其他答案中获得帮助。这是代码。

private void setContactRingtone(String number) {
    final Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
// The columns used for `Contacts.getLookupUri`
    final String[] projection = new String[]{
            ContactsContract.Contacts._ID, ContactsContract.Contacts.LOOKUP_KEY
    };
// Build your Cursor
    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;
        }

        File file = new File(this.getFilesDir() + File.separator + "ringtone.mp3");
        final String value = Uri.fromFile(file).toString();

        final ContentValues values = new ContentValues(1);
        values.put(ContactsContract.Contacts.CUSTOM_RINGTONE, value);
        getContentResolver().update(contactUri, values, null, null);
        Toast.makeText(getApplicationContext(), "Done!!!!!!!!!!!", Toast.LENGTH_SHORT).show();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        // Don't forget to close your Cursor
        data.close();
    }
 }

代码未将用户定义的铃声分配给联系人。没有生成异常。

【问题讨论】:

    标签: android android-studio


    【解决方案1】:

    所以问题出在 android 10 资产文件夹中,可通过 inputstream 访问。Referred this answer

    这里我们创建一个临时文件并写入外部存储。然后读取那个文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-09
      • 1970-01-01
      • 2011-05-18
      • 2019-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多