简体   繁体   中英

Android : Alert dialog with sound file

I have an alert dialog that pop ups before entering the activity. It is an alert dialog with a chechbox of Don't Show this again . I added a mp3 file that reads all terms. I tried all possible things, it didn't work. Here's what i want to do.

  1. When I Agree button is clicked, it should stop the sound file being played.

  2. When user clicks on Don't show this again CheckBox, the sound should not play after entering the activity again.

Every single clue will be deeply appriciated. Thnx in advance.

   }

    @Override
    protected void onResume() {
        AlertDialog.Builder warning1 =     new AlertDialog.Builder(this);
        LayoutInflater warning1Inflater =     LayoutInflater.from(this);
        View myLayout = warning1Inflater.inflate(R.layout.warning_layout, null);
        final CheckBox dontShowAgain = (CheckBox) myLayout.findViewById(R.id.skip);
         warning1 .setCancelable(false); 
         warning1 .setView(myLayout);
         warning1 .setTitle("Warning!");


        MediaPlayer mp = MediaPlayer.create(StartDialog.this,R.raw.terms);
        mp.start();


   warning1 .setIcon(R.drawable.alert1);
         warning1 .setPositiveButton("I Agree", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    String checkBoxResult = "NOT checked";
                    if (dontShowAgain.isChecked())
                        checkBoxResult = "checked";
                        String PREFS_NAME = null;
                        SharedPreferences settings =     getSharedPreferences(PREFS_NAME, 0);
                        SharedPreferences.Editor editor = settings.edit();
                    editor.putString("skipMessage", checkBoxResult);

                    editor.commit();

    MediaPlayer mp = MediaPlayer.create(StartDialog.this,R.raw.terms);
        mp.stop();


      return;

                }
            });


            String PREFS_NAME = null;
            SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
            String skipMessage = settings.getString("skipMessage", "NOT checked");
        if (!skipMessage.equals("checked"))
             warning1 .show();

        super.onResume();

You should implement this one.

dontShowAgain.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            // TODO Auto-generated method stub
            switch (buttonView.getId()) {
            case R.id.checkBox:
                checkBoxResult = "checked";
                String PREFS_NAME = "";
                SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                SharedPreferences.Editor editor = settings.edit();
                editor.putString("skipMessage", checkBoxResult);
                editor.commit();
                break;
            }
        }
    });

And click on OK button you have to dismiss your Dialog .

Now you can check it by

  SharedPreferences sp = getSharedPreferences("PREFS_NAME", 0);

  boolean hasBeenChecked = sp.getBoolean("skipMessage", false);
  if (!hasBeenChecked) {
                show your dialog here
  } else {
                // do your stuff here
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM