重新启动后,Android播放默认通知而不是自定义(保存在sharedPreferences中)

时间:2019-07-19 01:29:56

标签: java android notifications sharedpreferences ringtone

三种方法:ringtone(); onActivityResult(); saveRingtone();做工作。 在应用程序的第一次加载期间,第四种方法loadRingtone();也可以使用,它可以重复播放用户选择的任何声音。 但是随着应用程序重启: 它确实保存并带回用户选择的通知声音的字符串。但是,正如它应该播放从该字符串转换而来的声音一样,它会播放默认声音。

我用一些烤面包来检查这些值。     Toast.makeText(this,uri.toString(),Toast.LENGTH_SHORT).show();     Toast.makeText(this,ringPath,Toast.LENGTH_SHORT).show();

public void ringtone(View view){
        final Uri currentTone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, currentTone);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
        this.startActivityForResult(intent, 5);
    }
    @Override
    protected void onActivityResult(final int requestCode, final int resultCode, final Intent intent) {
        if (resultCode == Activity.RESULT_OK && requestCode == 5){
            uri = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
            if (uri == null) {
                uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            }
            sound = RingtoneManager.getRingtone(getApplicationContext(), uri);
            saveRingtone();
         }
    }
    public void saveRingtone(){
        SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(Ring, uri.toString());
        editor.apply();
     }
    public void loadRingtone(){
        SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
        ringPath = sharedPreferences.getString(Ring, "");
        if(ringPath == ""){
            uri =    RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
         }else {
             uri.parse(ringPath);
         }
         sound = RingtoneManager.getRingtone(getApplicationContext(), uri);
         sound.play();
    }

它应该播放在loadRingtone()中设置“ sound”变量的通知。方法(不是默认声音)。

0 个答案:

没有答案