如何在应用程序首次启动时显示警告对话框

How to show a alert dialog when app is launched first time

我的应用程序中有两个音频文件。当应用程序第一次启动时,当用户第一次播放第一个音频时,我想显示一个对话框,但之后再也不会显示了。

当用户点击确定后,只有对话框会消失。

我只创建了对话框的XML,因为我不知道第一次启动应用程序时如何显示布局。

MainActivity.java 这里的 player1(媒体播放器)和 play1(ImageView 作为播放音频的按钮)是第一个音频,对话框必须是显示。

public class MainActivity extends AppCompatActivity {
    MediaPlayer player1, player2;
    SeekBar seekBar1, seekBar2;
    TextView currentTime1, currentTime2;
    TextView remainingTime1, remainingTime2;
    ImageView play1, play2;
    int totalTime1, totalTime2;

    @Override

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

        // PlayButton    *  The ButtonClick is in the last if you want to jump directly there  *

        play1 = findViewById(R.id.playbtn);
        play2 = findViewById(R.id.playbtn2);

        // TimeLables

        currentTime1 = findViewById(R.id.currentTime1);
        currentTime2 = findViewById(R.id.currentTime2);
        remainingTime1 = findViewById(R.id.totalTime1);
        remainingTime2 = findViewById(R.id.totalTime2);

        // MediaPlayer

        player1 = MediaPlayer.create(this, R.raw.dog_howl);
        player2 = MediaPlayer.create(this, R.raw.dog_bark);
        player1.setLooping(false);
        player1.seekTo(0);
        totalTime1 = player1.getDuration();
        player2.setLooping(false);
        player2.seekTo(0);
        totalTime2 = player2.getDuration();


        //SeekBar

        seekBar1 = findViewById(R.id.seekbar1);
        seekBar2 = findViewById(R.id.seekbar2);
        seekBar1.setMax(totalTime1);
        seekBar2.setMax(totalTime2);

        seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                player1.seekTo(progress);
                seekBar1.setProgress(progress);
                currentTime1.setText(createTimerLable1(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {


            }
        });
        seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                player2.seekTo(i);
                seekBar2.setProgress(i);
                currentTime2.setText(createTimerLable2(i));


            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {


            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {


            }
        });


        new Thread(() -> {
            while (player1 != null) {
                try {
                    Message msg = new Message();
                    msg.what = player1.getCurrentPosition();
                    handler1.sendMessage(msg);
                    Thread.sleep(1000000000);
                } catch (InterruptedException ignored) {

                }
            }
        }).start();

        new Thread(() -> {
            while (player2 != null) {
                try {
                    Message msg = new Message();
                    msg.what = player2.getCurrentPosition();
                    handler2.sendMessage(msg);
                    Thread.sleep(1000000000);
                } catch (InterruptedException ignored) {

                }
            }
        }).start();


        // Admob Banner Ad

        MobileAds.initialize(this, initializationStatus -> {
        });

        AdView mAdView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

    }

    @SuppressLint("HandlerLeak")
    private final Handler handler1 = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            int currentPosition1 = msg.what;
            //Update SeekBar
            seekBar1.setProgress(currentPosition1);
            // Update Timelable
            String totTime1 = createTimerLable1(player1.getDuration());
            remainingTime1.setText(totTime1);

        }
    };
    @SuppressLint("HandlerLeak")
    private final Handler handler2 = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            int currentPosition2 = msg.what;
            // Update SeekBar
            seekBar2.setProgress(currentPosition2);
            // Update Timelable
            String totTime2 = createTimerLable2(player2.getDuration());
            remainingTime2.setText(totTime2);

        }
    };

    public String createTimerLable1(int duration) {
        String timerLabel = "";
        int min = duration / 1000 / 60;
        int sec = duration / 1000 % 60;
        timerLabel += min + ":";
        if (sec < 10) timerLabel += "0";
        timerLabel += sec;
        return timerLabel;


    }

    public String createTimerLable2(int duration) {
        String timerLabel = "";
        int min = duration / 1000 / 60;
        int sec = duration / 1000 % 60;
        timerLabel += min + ":";
        if (sec < 10) timerLabel += "0";
        timerLabel += sec;
        return timerLabel;


    }

    public void playBtnClick1(View view) {

        if (player2.isPlaying()) {
            player2.pause();
            play2.setImageResource(R.drawable.ic_baseline_play_circle_filled_24);
        }

        if (!player1.isPlaying()) {
            // Stoping
            player1.start();
            play1.setImageResource(R.drawable.ic_baseline_pause_circle_filled_24);
        } else {
            // Playing
            player1.pause();
            play1.setImageResource(R.drawable.ic_baseline_play_circle_filled_24);
        }

    }

    public void playBtnClick2(View view) {

        if (player1.isPlaying()) {
            player1.pause();
            play1.setImageResource(R.drawable.ic_baseline_play_circle_filled_24);
        }

        if (!player2.isPlaying()) {
            // Stoping
            player2.start();
            play2.setImageResource(R.drawable.ic_baseline_pause_circle_filled_24);
        } else {
            // Playing
            player2.pause();
            play2.setImageResource(R.drawable.ic_baseline_play_circle_filled_24);
        }

    }

}

正如@blackapps 所建议的,您将在 SharedPreferences 中保存有关用户的信息。您将保存是否显示对话框以及用户是否首次播放音频。

要从 SharedPreferences 保存或获取详细信息,

  1. 你可以按照this SO answer存储字符串、int、boolean等
  2. 您可以按照this SO answer存储自定义对象。