2015-09-05 42 views
1

在第一個活動中,意圖選擇的鈴聲被創建,而在第二個活動的按鈕按下時,我想播放從第一個活動中進行的選擇。我沒有收到任何錯誤,第二項活動中的按鈕什麼都不做。播放來自首選項的鈴聲不工作,從第二個活動按鈕上調用沒有錯誤

下面是相關代碼:

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(); 




       }});} 
       } 
+0

按鈕2用於第一個活動中的鈴聲選擇,playtbutton用於第二個活動。 – Mike

+0

我得到的代碼在發佈的問題中提出的工作。如何返回結果始終播放默認的鈴聲,而不是我可以通過烤麪包驗證選定的。路徑顯示如下:content:// media/internal/audio/media/50。 – Mike

回答

0

在上述評論的光,我將關閉這個問題,並試圖解決的最後一個問題。希望以下代碼可以幫助其他人面對這個問題。

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>