2015-12-21 103 views
0

我使用搜索欄對我的應用程序的Soundpool音量控制,我用PopupWindow的搜索條,一切工作正常,除了,每當我更改或暫停我的活動,我再次打開搜索條popupwindow,搜索條無我上次保存的價值越長。但它總是返回到它的默認值100%,甚至我用SharedPreferences什麼效果很好,但只有當我不離開當前活動。Android的搜索條進度不更新

public class ActivityMain extends Activity implements OnTouchListener, OnMenuItemClickListener { 

SoundManager snd; 
OnSeekBarChangeListener barChange1; 

private int soundID; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(R.layout.main_activity); 
     } 

public void onRestart() { 
    super.onRestart(); 

} 

public void onResume() { 
    super.onResume(); 



    final Button btnOpenPopup = (Button)findViewById(R.id.button6); 
    btnOpenPopup.setOnClickListener(new Button.OnClickListener(){ 

     @Override 
     public void onClick(View arg0) { 
     LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE); 
     View popupView = layoutInflater.inflate(R.layout.popup, null); 
     final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 


        barChange1 = new OnSeekBarChangeListener() 
        { 


         @Override 
         public void onStopTrackingTouch(SeekBar seekBar) { 
         SharedPreferences prefs = getBaseContext().getSharedPreferences("mySharedPrefsFilename1", Context.MODE_PRIVATE); 
         prefs.edit().putInt("seekBarValue", seekBar.getProgress()).commit();    

         } 

         @Override 
         public void onStartTrackingTouch(SeekBar seekBar) { } 

         @Override 
         public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 

           snd.setVolume((float)progress/100.0f); 

          } 
         }; 

        SeekBar volbar1 = (SeekBar)popupView.findViewById(R.id.VolBar1);     
        volbar1.setMax(100);      
        int value = 0; 
        SharedPreferences prefs = getBaseContext().getSharedPreferences("mySharedPrefsFilename1", Context.MODE_PRIVATE); 
        value = prefs.getInt("seekBarValue", 100); 
        volbar1.setProgress(value); 
        volbar1.setOnSeekBarChangeListener(barChange1); 


        Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss); 
        btnDismiss.setOnClickListener(new Button.OnClickListener(){ 

      @Override 
      public void onClick(View v) { 
      // TODO Auto-generated method stub 
      popupWindow.dismiss(); 


        }}); 

          popupWindow.showAsDropDown(btnOpenPopup, 60, 20); 

        }}); 
         { 
       } 



     this.setVolumeControlStream(AudioManager.STREAM_MUSIC); 

     snd = new SoundManager(getApplicationContext()); 

     soundID = snd.load(R.raw.sound_1); 



ImageView img01 = (ImageView) findViewById(R.id.imageView11); 
img01.setOnTouchListener(new OnTouchListener() {  

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     if (event.getAction() == MotionEvent.ACTION_DOWN) { 
    } 

       snd.play(soundID); 
       return false; 
     }   
    }); 
} 



     @Override 
     public boolean onMenuItemClick(MenuItem item) { 
      // TODO Auto-generated method stub 
      return false; 
     } 


     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      // TODO Auto-generated method stub 
      return false; 
     } 



     @Override 
     protected void onPause() { 
      super.onPause(); 
      snd.unloadAll(); 



     } 
    } 

回答

0

將這個的onResume()

 int value = 0; 
    SharedPreferences prefs = getBaseContext().getSharedPreferences("mySharedPrefsFilename1", Context.MODE_PRIVATE); 
    value = prefs.getInt("seekBarValue", 100) 
    volbar1.setProgress(value); 
+0

嗨,我嘗試,但它使我的應用程序崩潰..必須測試更多的,也許是我做錯了什麼.. – Blackstarguy

+0

volbar1很可能無法初始化爲此volbar1 =(搜索欄)popupView.findViewById(R.id .VolB) –

+0

這就奇怪了,其實,搜索欄的作品,因爲它應該但從設備返回音量總是默認爲100%,如果我暫停或更改活動.. – Blackstarguy

0

我做同樣的事情。它爲我工作。這是我的代碼。它沒有所有的聲音的東西。

package com.titlesource.testproject; 

import android.content.Context; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.LinearLayoutCompat; 
import android.support.v7.widget.Toolbar; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.Button; 
import android.widget.PopupWindow; 
import android.widget.SeekBar; 

public class ActivityMain extends AppCompatActivity { 

    SeekBar.OnSeekBarChangeListener barChange1; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_activity_main); 
    } 

    public void onRestart() { 
     super.onRestart(); 

    } 

    public void onResume() { 
     super.onResume(); 


     final Button btnOpenPopup = (Button)findViewById(R.id.button6); 
     btnOpenPopup.setOnClickListener(new Button.OnClickListener(){ 

      @Override 
      public void onClick(View arg0) { 
       LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE); 
       View popupView = layoutInflater.inflate(R.layout.popup, null); 
       final PopupWindow popupWindow = new PopupWindow(popupView, LinearLayoutCompat.LayoutParams.WRAP_CONTENT, LinearLayoutCompat.LayoutParams.WRAP_CONTENT); 


       barChange1 = new SeekBar.OnSeekBarChangeListener() 
       { 


        @Override 
        public void onStopTrackingTouch(SeekBar seekBar) { 
         SharedPreferences prefs = getBaseContext().getSharedPreferences("mySharedPrefsFilename1", Context.MODE_PRIVATE); 
         prefs.edit().putInt("seekBarValue", seekBar.getProgress()).commit(); 

        } 

        @Override 
        public void onStartTrackingTouch(SeekBar seekBar) { } 

        @Override 
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 

        } 
       }; 

       SeekBar volbar1 = (SeekBar)popupView.findViewById(R.id.VolBar1); 
       volbar1.setMax(100); 
       int value = 0; 
       SharedPreferences prefs = getBaseContext().getSharedPreferences("mySharedPrefsFilename1", Context.MODE_PRIVATE); 
       value = prefs.getInt("seekBarValue", 100); 
       volbar1.setProgress(value); 
       volbar1.setOnSeekBarChangeListener(barChange1); 


       Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss); 
       btnDismiss.setOnClickListener(new Button.OnClickListener(){ 

        @Override 
        public void onClick(View v) { 
         // TODO Auto-generated method stub 
         popupWindow.dismiss(); 


        }}); 

       popupWindow.showAsDropDown(btnOpenPopup, 60, 20); 

      }}); 
     { 
     } 
    } 
}