未以编程方式设置铃声和联系人铃声

时间:2015-12-11 16:43:19

标签: java android xml android-intent android-activity

我正在制作一个应用程序,我从服务器下载铃声并保存在SD卡/下载并设置为闹钟,通知,铃声和联系音但但当我设置它不设置为联系和铃声但通知,警报音设置在哪里是我的代码中的错误其次通过/ sdcard / download / tones不通过/下载/

保存在ome mobile中
    ringtone.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String path = Environment.getExternalStorageDirectory().getPath() + "/Download/" + title;
                        setRingtone(path);
                        Toast.makeText(con, "set successfully as ringtone"+title, Toast.LENGTH_LONG).show();
                        dialog.dismiss();

                    }
                });

                alaram.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String path = Environment.getExternalStorageDirectory().getPath() + "/Download/" + title;
                        setalaram(path);
                        Toast.makeText(con, "set successfully as alaram", Toast.LENGTH_LONG).show();
                        dialog.dismiss();

                    }
                });

                notification.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String path = Environment.getExternalStorageDirectory().getPath() + "/Download/" + title;
                        setnotication(path);
                        Toast.makeText(con, "set successfully as notification", Toast.LENGTH_LONG).show();
                        dialog.dismiss();

                    }
                });
               /* contact.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                    String path = Environment.getExternalStorageDirectory().getPath() + "/Download/" + title;
                        Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                        startActivityForResult(intent, PICK_CONTACT);
                        dialog.dismiss();
                    }
                });*/

            }
        });
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode)
        {
            case 1:
                if (resultCode == Activity.RESULT_OK)
                    urlRingTone = String.valueOf(data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI));
                    break;
                case 2:
                        Uri contactData = data.getData();
                        Cursor c = managedQuery(contactData, null, null, null, null);
                        if (c.moveToFirst()) {
                            String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                            String path = Environment.getExternalStorageDirectory().getPath() + "/Download/" + title;
                            //setContact(path);
                        }

            }

        }
public void setalaram(String path) {


        File k = new File(path); // path is a file to /sdcard/media/ringtone

        ContentValues values = new ContentValues();
        values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
        values.put(MediaStore.MediaColumns.TITLE, title);
        values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
        values.put(MediaStore.MediaColumns.SIZE, k.length());
        values.put(MediaStore.Audio.Media.IS_ALARM, true);

        Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
        getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);
        Uri newUri = getContentResolver().insert(uri, values);

        RingtoneManager.setActualDefaultRingtoneUri(Play.this,
                RingtoneManager.TYPE_ALARM, newUri);
    }

    public void setnotication(String path) {


        File k = new File(path); // path is a file to /sdcard/media/ringtone

        ContentValues values = new ContentValues();
        values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
        values.put(MediaStore.MediaColumns.TITLE, title);
        values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
        values.put(MediaStore.MediaColumns.SIZE, k.length());
        values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);

        Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
        getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);
        Uri newUri = getContentResolver().insert(uri, values);

        RingtoneManager.setActualDefaultRingtoneUri(Play.this,
                RingtoneManager.TYPE_NOTIFICATION, newUri);
    }

1 个答案:

答案 0 :(得分:0)

请记住,对于铃声必须使用:<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="showList1">List 1 <ul id="list1"> <li class="info1">Show info 1</li> <li class="info2">Show info 2</li> <li class="info3">Show info 3</li> <li class="info4">Show info 4</li> <li class="info5">Show info 5</li> </ul> <div class="info-box list1"> <ul> <li class="info1-display">Info 1</li> <li class="info2-display">Info 2</li> <li class="info3-display">Info 3</li> <li class="info4-display">Info 4</li> <li class="info5-display">Info 5</li> </ul> </div> </div> <div class="showList2">List 2 <ul id="list2"> <li class="info1">Show info 6</li> <li class="info2">Show info 7</li> <li class="info3">Show info 8</li> <li class="info4">Show info 9</li> <li class="info5">Show info 10</li> </ul> <div class="info-box list2"> <ul> <li class="info1-display">Info 6</li> <li class="info2-display">Info 7</li> <li class="info3-display">Info 8</li> <li class="info4-display">Info 9</li> <li class="info5-display">Info 10</li> </ul> </div> </div>

RingtoneManager.TYPE_RINGTONE

RingtoneManager.setActualDefaultRingtoneUri(
  Play.this,
  RingtoneManager.TYPE_RINGTONE,
  newUri
);  

检查这个完整的例子:

 values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);