如何在我的AndroNoise中添加AlertDialog



我之前问过一个关于更改方法OnClick到方法生命周期的问题

但我认为问题已经解决了。

但在这个新的问题上和以前一样但不同。

现在我要问如何将alertdialog添加到我的源代码中。我有3个aletdialog会显示噪音直到70分贝,80分贝和100分贝。

这是我的代码

package com.andronoise;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.SharedPreferences;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.ToggleButton;
import java.text.DecimalFormat;
public class MainActivity extends Activity implements InputListener{
  InputSuara micInput;
  TextView mdBTextView;
  TextView mdBFractionTextView;
  LevelBar mBarLevel;
  double mGain = 2500.0 / Math.pow(10.0, 90.0 / 20.0);
  double mRmsSmoothed;
  double mAlpha = 0.9;
  public double dB;
  private int mSampleRate;
  private int mAudioSource;
  private volatile boolean mDrawing;
  private volatile int mDrawingCollided;
  private static final String TAG = "MainActivity";
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    micInput = new InputSuara(this);
    
    setContentView(R.layout.activity_main);
    mBarLevel = (LevelBar)findViewById(R.id.bar_level_drawable_view);
    mdBTextView = (TextView)findViewById(R.id.dBTextView);
    mdBFractionTextView = (TextView)findViewById(R.id.dBFractionTextView);
    final ToggleButton onOffButton=(ToggleButton)findViewById(
        R.id.on_off_toggle_button);
    ToggleButton.OnClickListener tbListener =
        new ToggleButton.OnClickListener() {
      @Override
      public void onClick(View v) {
    	  
        if (onOffButton.isChecked()) {
          readPreferences();
          micInput.setSampleRate(mSampleRate);
          micInput.setAudioSource(mAudioSource);
          micInput.start();
          
        } else {
          micInput.stop();
        }
     
      }
      
    };
    onOffButton.setOnClickListener(tbListener);     
      }  
  private void readPreferences() {
    SharedPreferences preferences = getSharedPreferences("LevelMeter",
        MODE_PRIVATE);
    mSampleRate = preferences.getInt("SampleRate", 8000);
    mAudioSource = preferences.getInt("AudioSource", 
        MediaRecorder.AudioSource.VOICE_RECOGNITION);
  }
  
  public void Alert () {
	  
	  if (dB >= 70 || dB <80) {
	      	  AlertDialog.Builder build = new
	      			  AlertDialog.Builder(MainActivity.this);
	      			      build.setTitle("Alert!!!").setIcon(R.drawable.ic_launcher).setMessage("Level 70 dB, kebisingan dalam tingkat rendah")
	      			      .setPositiveButton("Close",null).show();
	        }
	        else if (dB >=80 || dB <100) {
	      	  AlertDialog.Builder build = new
	      			  AlertDialog.Builder(MainActivity.this);
	      			      build.setTitle("Alert!!!").setIcon(R.drawable.ic_launcher).setMessage("Level 80 dB, kebisingan dalam tingkat sedang Waspada !")
	      			      .setPositiveButton("Close",null).show();
	        }
	        else if ( dB >= 100) {
	      	  AlertDialog.Builder build = new
	      			  AlertDialog.Builder(MainActivity.this);
	      			      build.setTitle("Alert!!!").setIcon(R.drawable.ic_launcher).setMessage("Level 100 dB, berbahaya jika lebih dari 1 menit!!! Segera menghindar")
	      			      .setPositiveButton("Close",null).show();
	        };
  }
  public void processAudioFrame(short[] audioFrame) {
    if (!mDrawing) {
      mDrawing = true;
      double rms = 0;
      for (int i = 0; i < audioFrame.length; i++) {
        rms += audioFrame[i]*audioFrame[i];
      }
      rms = Math.sqrt(rms/audioFrame.length);
      mRmsSmoothed = mRmsSmoothed * mAlpha + (1 - mAlpha) * rms;
      final double rmsdB = 20.0 * Math.log10(mGain * mRmsSmoothed);
      dB = 20 + rmsdB;
      
      mBarLevel.post(new Runnable() {
    	  @Override
        public void run() {
          mBarLevel.setLevel(( 10 + rmsdB) / 90);
          DecimalFormat df = new DecimalFormat("##");
          mdBTextView.setText(df.format(dB));
          
          int one_decimal = (int) (Math.round(Math.abs(rmsdB * 10))) % 10;
          mdBFractionTextView.setText(Integer.toString(one_decimal));
          mDrawing = false;
   	}
          });
      
    } else {
      mDrawingCollided++;
      Log.v(TAG, "Level bar update collision, i.e. update took longer " +
          "than 20ms. Collision count" + Double.toString(mDrawingCollided));
    }
  }  
}

如果条件为,则您的错误

     if (dB >= 70 || dB <80){
    //do something
    }              
     else if (dB >=80 || dB <100){
    //do something
    }

例如db=95如果,应用程序将转到第一个

正确的代码是:

    if (dB >= 70 && dB <80){
    //do something
    } else if (dB >=80 && dB <100){
    //do something
    }else{
    //do something
     }

所以

        package com.andronoise;
    import android.content.DialogInterface;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.SharedPreferences;
    import android.media.MediaRecorder;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.TextView;
    import android.widget.ToggleButton;
    import java.text.DecimalFormat;
    public class MainActivity extends Activity implements InputListener{
      InputSuara micInput;
      TextView mdBTextView;
      TextView mdBFractionTextView;
      LevelBar mBarLevel;
      double mGain = 2500.0 / Math.pow(10.0, 90.0 / 20.0);
      double mRmsSmoothed;
      double mAlpha = 0.9;
      public double dB;
      private int mSampleRate;
      private int mAudioSource;
      private volatile boolean mDrawing;
      private volatile int mDrawingCollided;
      private static final String TAG = "MainActivity";
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        micInput = new InputSuara(this);
        setContentView(R.layout.activity_main);
        mBarLevel = (LevelBar)findViewById(R.id.bar_level_drawable_view);
        mdBTextView = (TextView)findViewById(R.id.dBTextView);
        mdBFractionTextView = (TextView)findViewById(R.id.dBFractionTextView);
        final ToggleButton onOffButton=(ToggleButton)findViewById(
            R.id.on_off_toggle_button);
        ToggleButton.OnClickListener tbListener =
            new ToggleButton.OnClickListener() {
          @Override
          public void onClick(View v) {
            if (onOffButton.isChecked()) {
              readPreferences();
              micInput.setSampleRate(mSampleRate);
              micInput.setAudioSource(mAudioSource);
              micInput.start();
            } else {
              micInput.stop();
            }
          }
        };
        onOffButton.setOnClickListener(tbListener);     
          }  
      private void readPreferences() {
        SharedPreferences preferences = getSharedPreferences("LevelMeter",
            MODE_PRIVATE);
        mSampleRate = preferences.getInt("SampleRate", 8000);
        mAudioSource = preferences.getInt("AudioSource", 
            MediaRecorder.AudioSource.VOICE_RECOGNITION);
      }
      public void Alert () {
        if (dB >= 70 && dB <80) {
            AlertDialog.Builder build = new
                    AlertDialog.Builder(MainActivity.this);
            build.setTitle("Alert!!!").setIcon(R.drawable.ic_launcher).setMessage("Level 70 dB, kebisingan dalam tingkat rendah")
                    .setPositiveButton("Close", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    }).show();

        }
        else if (dB >=80 && dB <100) {
            AlertDialog.Builder build = new
                    AlertDialog.Builder(MainActivity.this);
            build.setTitle("Alert!!!").setIcon(R.drawable.ic_launcher).setMessage("Level 80 dB, kebisingan dalam tingkat sedang Waspada !")
                    .setPositiveButton("Close",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    }).show();
        }
        else if ( dB >= 100) {
            AlertDialog.Builder build = new
                    AlertDialog.Builder(MainActivity.this);
            build.setTitle("Alert!!!").setIcon(R.drawable.ic_launcher).setMessage("Level 100 dB, berbahaya jika lebih dari 1 menit!!! Segera menghindar")
                    .setPositiveButton("Close",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    }).show();
        };
}
      public void processAudioFrame(short[] audioFrame) {
        if (!mDrawing) {
          mDrawing = true;
          double rms = 0;
          for (int i = 0; i < audioFrame.length; i++) {
            rms += audioFrame[i]*audioFrame[i];
          }
          rms = Math.sqrt(rms/audioFrame.length);
          mRmsSmoothed = mRmsSmoothed * mAlpha + (1 - mAlpha) * rms;
          final double rmsdB = 20.0 * Math.log10(mGain * mRmsSmoothed);
          dB = 20 + rmsdB;
          mBarLevel.post(new Runnable() {
              @Override
            public void run() {
              mBarLevel.setLevel(( 10 + rmsdB) / 90);
              DecimalFormat df = new DecimalFormat("##");
              mdBTextView.setText(df.format(dB));
              Alert();
              int one_decimal = (int) (Math.round(Math.abs(rmsdB * 10))) % 10;
              mdBFractionTextView.setText(Integer.toString(one_decimal));
              mDrawing = false;
        }
              });
        } else {
          mDrawingCollided++;
          Log.v(TAG, "Level bar update collision, i.e. update took longer " +
              "than 20ms. Collision count" + Double.toString(mDrawingCollided));
        }
      }  
    }

最新更新