从首选项播放铃声不起作用,从第二个 activity 调用按钮按下没有错误

Play Ringtone from preference not working, called from second activity on button press no errors

在第一个活动中,制作了 select 的铃声,在第二个 activity 按钮按下时,我想播放第一个 [=16] 制作的 selection =].我没有收到任何错误,第二个 activity 中的按钮什么也没做。

相关代码如下:

    Button button2 = (Button)findViewById(R.id.button2);

    button2.setOnClickListener(new Button.OnClickListener(){


    @Override
    public void onClick(View v) {
    Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,
    RingtoneManager.TYPE_NOTIFICATION);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select
    Ringtone");
    if (mRingtoneUri != null) { 
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, 
    Uri.parse((String) mRingtoneUri));
    } else {
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, 
    (Uri) null);
    }
    startActivityForResult(intent, RINGTONE_REQUEST);
      }
      });
     }   

     protected void onActivityResult(int requestCode, int resultCode,
     Intent data, final String mRingtoneUri, Object RINGTONE, int
      RINGTONE_REQUEST) {
       if (requestCode == RINGTONE_REQUEST && resultCode == RESULT_OK) {
        final Uri uri = 
      data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
        String PREF = null;
        SharedPreferences preferences = getSharedPreferences(PREF, 
       MODE_PRIVATE);
        Editor editor = preferences.edit();
        if (uri == null)
            editor.putString((String) RINGTONE, null);
        else
            editor.putString((String) RINGTONE, uri.toString());
         editor.commit();


        Button PlayButton = (Button)  
        findViewById(R.id.Playbutton);
        PlayButton.setOnClickListener(new OnClickListener() {

            private Context context;

            @Override
            public void onClick(View view) {
                try {
                    mp.setDataSource(mRingtoneUri);
                } catch (IllegalArgumentException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (SecurityException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IllegalStateException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();

                  }
                try {
                    mp.prepare();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                 MediaPlayer.create(context, uri);
                mp.release();
                finish();




                 }});}
                }

鉴于以上评论,我将关闭此问题并尝试解决最后一个问题。希望以下代码可以帮助其他人解决这个问题。

Preference.xml

<?xml version="1.0" encoding="utf-8"?>

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res
/android"
android:title="Settings">


 <PreferenceCategory android:title="Seconed Category">
    <RingtonePreference android:showDefault="true"
         android:key="ringtone" 
        android:title="Select sound"
        android:ringtoneType="ringtone">
    </RingtonePreference>

  </PreferenceCategory>
 </PreferenceScreen>

Main.xml

public class MyActivity extends Activity {

protected MediaPlayer mp;
protected String mRingtoneUri;
protected Context context;
protected Uri uri;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    ((Button)findViewById(R.id.Button01)).setOnClickListener(new 
    OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent settingsActivity = new Intent(getBaseContext(),
                    PreferencesActivityTest.class);
            startActivity(settingsActivity);

        }
    });
    }

   @Override
   protected void onResume() {
    super.onResume();
    PreferenceManager.getDefaultSharedPreferences(this);


  {

  }


  {
 ((Button)findViewById(R.id.button3)).setOnClickListener(new 
 OnClickListener() {



            @Override
            public void onClick(View v) {

                SharedPreferences getAlarms = 
      PreferenceManager.getDefaultSharedPreferences(getBaseContext());
                String alarms = getAlarms.getString("ringtone", "");
                    Uri.parse(alarms);


                Toast.makeText(getApplicationContext(), alarms, 
      Toast.LENGTH_LONG).show();


                Uri notification =   
RingtoneManager.getActualDefaultRingtoneUri(getApplicationContext(),
 RingtoneManager.TYPE_NOTIFICATION);
                MediaPlayer mp = 
MediaPlayer.create(getApplicationContext(), notification);
                mp.start();


                         }

                           });}}}


 Main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">

<Button android:text="Show RingTone Preferences"   
android:id="@+id/Button01"
    android:layout_width="wrap_content" 
android:layout_height="wrap_content"></Button>


<Button 
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Play" />

 </LinearLayout>