新手 - 在我的活动进入后台后,我无法了解我的枚举或被盗的状态?

时间:2016-06-04 09:32:05

标签: android enums android-mediaplayer

我有应用程序在哪里播放我的三个音乐文件之一。活动进入后台后,我重新打开此UI活动切换按钮,来自"关闭"即使我上次改变了它的状态。顺便说一句,我阅读android开发人员文档,看起来很复杂。

我应该怎么做?

package com.example.casperr.sounds;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;


public class Listening extends AppCompatActivity {
    public MediaPlayer oursong;
    boolean bool ;
    private SeekBar mSeekBarVolume;


    enum State{
        Retrieving,
        Stopped,
        Preparing,
        Playing,
        Paused
    };
    State mState = State.Retrieving;
    public AudioManager myAudioManager;



    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.listening);
        final ToggleButton toggle = (ToggleButton) findViewById(R.id.toggleButton);
        toggle.setBackgroundColor(Color.parseColor("#26A69A"));

        Intent ourintent = getIntent();
        int dek1 = ourintent.getIntExtra("dek", 0);
               ImageView img = (ImageView) findViewById(R.id.imageView3);
        if (dek1 == 1) {
            img.setImageResource(R.drawable.a);

        } else if (dek1 == 2) {
            img.setImageResource(R.drawable.b);

        } else if (dek1 == 3) {
            img.setImageResource(R.drawable.c);

        }
        SeekBar seekbar = (SeekBar)findViewById(R.id.seekbar); // make seekbar object
        seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onStopTrackingTouch(SeekBar arg0) {
            }

            @Override
            public void onStartTrackingTouch(SeekBar arg0) {

            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
                float volume = (float) (1 - (Math.log(100 - progress) / Math.log(100)));
                oursong.setVolume(volume, volume);
            }});

            toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        if(oursong==null) {
                            Intent ourintent = getIntent();
                            int dek1 = ourintent.getIntExtra("dek", 0);
                            if (dek1 == 1) {

                                oursong = MediaPlayer.create(Listening.this, R.raw.a);
                            } else if (dek1 == 2) {

                                oursong = MediaPlayer.create(Listening.this, R.raw.b);
                            } else if (dek1 == 3) {

                                oursong = MediaPlayer.create(Listening.this, R.raw.c);
                            }
                            oursong.start();
                            bool=true;

                        }else {
                            toggle.setChecked(true);
                        }

                        mState = State.Playing;
                    } else {
                        stopPlaying();
                    }
                }
            });


    }
    public void onPause() { super.onPause();
           /**oursong.stop();**/}
    public void onStart() {
        super.onStart();
        final ToggleButton toggle = (ToggleButton) findViewById(R.id.toggleButton);

        if(bool=true){toggle.setChecked(true);}else{toggle.setChecked(false);}

    }

    private void stopPlaying() {
        if (oursong != null) {
            oursong.stop();
            oursong.release();
            oursong = null;
        }
    }

1 个答案:

答案 0 :(得分:0)

您可以使用sharedPreferences将该布尔值的值与缓存相同。在onPause()方法

中执行此操作
Context context = getActivity();
SharedPreferences sharedPref = context.getSharedPreferences(Context.MODE_PRIVATE);

SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("MyBoolean", bool_x );
editor.commit();

然后当你重新打开活动时,从共享偏好中读取布尔值,如此

 SharedPreferences sharedPref = context.getSharedPreferences(Context.MODE_PRIVATE);
 boolean myBoolean = sharedPref.getBoolean("MyBoolean" , defaultValue ); 
 // the default value can be anything and is used if the SharedPreferences where empty

public void onPause() { super.onPause(); 
       /**oursong.stop();**/} 
public void onStart() { 
    super.onStart(); 
    final ToggleButton toggle = (ToggleButton) findViewById(R.id.toggleButton);

    if(bool=true){toggle.setChecked(true);}else{toggle.setChecked(false);}

    //     ^ use " == " not " = " which will always set the value of "bool" to true.