如何在androidstudio的单独活动中调用radiobutton中的信息

shstlldc  于 2021-07-07  发布在  Java
关注(0)|答案(1)|浏览(309)

在我的应用程序中,我可以在应用程序启动时播放音乐。如果用户转到“设置”,他们可以更改音量或打开和关闭音乐,但是我不知道如何从单选按钮中提取信息,以便在其他活动中打开或关闭音乐。我知道如果我在oncreate下关闭主活动中的音乐,它将收听单选按钮,但我希望在应用程序启动时播放音乐,当你恢复活动时,它会从单选按钮输入信息。
这是我主要活动的代码

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

public class MainActivity extends AppCompatActivity {

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

            Intent svc = new Intent(this, music.class
            );
            startService(svc);

        }

        public void onPlayPressed(View view) {

            Intent myIntent = new Intent(getBaseContext(), Game.class);
            startActivity(myIntent);

        }

        public void onSettingsPressed(View view) {

            Intent myIntent = new Intent(getBaseContext(), Settings.class);
            startActivity(myIntent);

        }

        public void onHowtoPressed(View view) {

            Intent myIntent = new Intent(getBaseContext(), HowtoPlay.class);
            startActivity(myIntent);

        }

        @Override
        public void onResume() {

            super.onResume();

        }

        @Override
        public void onDestroy() {

            super.onDestroy();
            Intent svc = new Intent(this, music.class);
            stopService(svc);

        }
    }

这是我的设置活动的代码

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.Toast;

public class Settings extends AppCompatActivity {

    private RadioGroup radioGroup;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
            final Intent svc = new Intent(this, music.class
            );

            RadioButton rdb = (RadioButton) findViewById(R.id.on);
            rdb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean checked = ((RadioButton) v).isChecked();
                    // Check which radiobutton was pressed
                    if (checked){
                        startService(svc);

                    }
                    else{
                        stopService(svc);
                    }
                }
            });
            RadioButton rdb2 = (RadioButton) findViewById(R.id.off);
            rdb2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean checked = ((RadioButton) v).isChecked();
                    // Check which radiobutton was pressed
                    if (checked){
                        stopService(svc);

                    }
                    else{
                        startService(svc);
                    }
                }
            });
            final AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        SeekBar volControl = (SeekBar)findViewById(R.id.seekBar);
        volControl.setMax(maxVolume);
        volControl.setProgress(curVolume);
        volControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onStopTrackingTouch(SeekBar arg0) {
            }

            @Override
            public void onStartTrackingTouch(SeekBar arg0) {
            }

            @Override
            public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
                audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, arg1, 0);
            }
        });

    }

        public void onMainMenuPressed(View view){

        Intent myIntent=new Intent(getBaseContext(),MainActivity.class);
        startActivity(myIntent);
    }

        public void onResumePressed (View view){

        Intent myIntent=new Intent(getBaseContext(),Game.class);
        myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        myIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(myIntent);
        finish();
    }
    } ```
c6ubokkw

c6ubokkw1#

将SharedReferences与简单布尔标志一起使用。
您应该在单选组上使用oncheckedchangelistener,而不是在单个单选按钮上使用onclicklistener,如下所示:

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(...) {
        getSharedPreferences("prefs", 0).edit().putBoolean("music", rdb.isChecked()).apply();
    }
});

然后在其他活动中:

boolean music = getSharedPreferences("prefs", 0).getBoolean("music", true);

然后检查标志的值以确定是否启动音乐服务。

相关问题