如果选中切换按钮,如何在服务类中获取seekbar的值

时间:2014-03-19 06:05:43

标签: android service broadcastreceiver sharedpreferences

我正在制作一个项目,我正在使用切换按钮和搜索栏。我可以使用共享prefrences保存切换按钮的值,但我无法使用搜索栏工作。在这里,我想要检查切换按钮,而不是只有广播接收器必须工作,广播接收器必须启动服务...我做的代码是......

在xml文件中我做了这个编码..

<SeekBar
    android:id="@+id/seekBar1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:progress="5"
    android:secondaryProgress="10"
    android:max="40"
    android:layout_marginTop="136dp" />

<ToggleButton
    android:id="@+id/toggleButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/seekBar1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="62dp"
    />

在Activity类中..

      TextView tv;
    SeekBar sb;
    SharedPreferences spref;
    SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sb=(SeekBar)findViewById(R.id.seekBar1);
        sb.setOnSeekBarChangeListener(this);
        tv=(TextView)findViewById(R.id.tv1);
        spref = PreferenceManager.getDefaultSharedPreferences(this);
        editor = spref.edit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
        // TODO Auto-generated method stub
        tv.setText(String.valueOf(sb.getProgress()));
        spref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        editor = spref.edit();
        int aprog=sb.getProgress();
        editor.putInt("mMySeekBarProgress", aprog).commit();

    }

    @Override
    public void onStartTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub

    }
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        int mProgress = spref.getInt("mMySeekBarProgress", 0);
        sb.setProgress(mProgress);
        sb.setSecondaryProgress(40);
    }

    @Override
    public void onStopTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub

    }

}

在BroadcastReceiver类中......

public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    boolean Enable = context.getSharedPreferences("pref", Context.MODE_PRIVATE).getBoolean("is toggle enable", false);
    int ee=context.getSharedPreferences("pref", Context.MODE_PRIVATE).getInt("sbar", 0);
    if(Enable){
        //Toast.makeText(context, "in on receive", Toast.LENGTH_SHORT).show();  
    Intent it = new Intent(context, myserve.class);

    it.putExtra("i", ee);
    context.startService(it);
}
}

在服务类中......

public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub
    super.onStart(intent, startId);
    //I want to use toast here to display value of seek bar
    int ii=intent.getExtras().getInt("i");
    Toast.makeText(getApplicationContext(), "i= "+ii, Toast.LENGTH_SHORT).show();
}

使用这个我能够保存togglebutton以及seekbar的状态,我能够获得进度值作为祝酒,但是当我退出活动并且我再次回来并做出一些改变时它不会显示吐司值,直到我将togglebutton从on更改为off并且反之亦然

1 个答案:

答案 0 :(得分:0)

我认为你在保存搜索栏的进度值方面犯了一个错误。 以下是捕获Toggle按钮和SeekBar状态的代码。

<强> MainActivity.java

 public class MainActivity extends Activity implements OnSeekBarChangeListener, OnCheckedChangeListener, OnClickListener{

private SeekBar mSeekBar;
private ToggleButton mToggleButton;
private int mSeekBarProgress;
private SharedPreferences mValuePrefs;
private Button mBtn;

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

    mSeekBar = (SeekBar)findViewById(R.id.seekBar1);
    mToggleButton = (ToggleButton)findViewById(R.id.toggleButton1);
    mBtn = (Button)findViewById(R.id.btn);
    mBtn.setOnClickListener(this);

    mValuePrefs = getSharedPreferences("prefs", MODE_PRIVATE);

    mSeekBar.setOnSeekBarChangeListener(this);
    mToggleButton.setOnCheckedChangeListener(this);
}

//SeekBar
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
        boolean fromUser) {
    mSeekBarProgress = progress;        
}


@Override
public void onStopTrackingTouch(SeekBar seekBar) {
    mSeekBarProgress = seekBar.getProgress();
    //Save value in shared prefs
    Editor editor = mValuePrefs.edit();
    editor.putInt("seekbar", mSeekBarProgress);
    editor.commit();
    }

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

//Toggle button
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
         //checked is true - save value in shared prefs
    Editor editor = mValuePrefs.edit();
    editor.putBoolean("toggle", isChecked);
    editor.commit();
    }

@Override
public void onClick(View v) {

    if(v.getId() == R.id.btn){
        Intent intent = new Intent(this,SecondActivity.class);
        startActivity(intent);
    }
}

}

SecondActivity.java 用于显示共享偏好设置中保存的值:

  public class SecondActivity extends Activity {

private static  String TAG = "SecondActivity";
private TextView mTxt;
private SharedPreferences mPrefs;

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

    mTxt = (TextView)findViewById(R.id.value);

    mPrefs = getSharedPreferences("prefs", MODE_PRIVATE);

    int seekbar = mPrefs.getInt("seekbar", 0);
    boolean toggle = mPrefs.getBoolean("toggle", false);

    Log.d(TAG,"Toggle "+toggle+" : SeekBar "+seekbar);

    mTxt.setText("Toggle "+toggle+" : SeekBar "+seekbar);

}

}