2

这是一个测量声音强度的应用程序,根据测量值设置搜索栏,然后根据搜索栏自动调整铃声音量。

当用户销毁它时,我想在前台和后台运行这个应用程序。因为用户即使在退出后也肯定会需要该应用程序。我已经阅读了创建服务的文档,但问题是我希望在应用程序销毁后在后台运行与活动相同的完整代码..所以任何帮助将不胜感激。

 package com.example.soundmeter;

 import android.app.Activity;
 import android.content.Intent;
 import android.media.MediaRecorder;
 import android.os.Bundle;
 import android.os.Handler;
 import android.util.Log;
 import android.widget.TextView;
 import android.content.Context;
 import android.media.AudioManager;
 import android.view.KeyEvent;
 import android.widget.SeekBar;
 import android.widget.SeekBar.OnSeekBarChangeListener;

 import com.example.soundmeter.R;

 public class MainActivity extends Activity {

 //public static boolean isService = false;     
 TextView mStatusView;
 MediaRecorder mRecorder;
 Thread runner;
 private static double mEMA = 0.0;
 static final private double EMA_FILTER = 0.6;

 //a variable to store the seek bar from the XML file  
 public SeekBar volumeBar; 

 //an AudioManager object, to change the volume settings  
 private AudioManager amanager; 

 final Runnable updater = new Runnable(){

     public void run(){          
         updateTv();
     };
 };
 final Handler mHandler = new Handler();

 public void onCreate(Bundle savedInstanceState) {


     setContentView(R.layout.main);
     super.onCreate(savedInstanceState);
     mStatusView = (TextView) findViewById(R.id.status);


     //startService(new Intent(MainActivity.this,BackGroundService.class));
     if (runner == null)
     { 
         runner = new Thread(){
             public void run()
             {
                 while (runner != null)
                 {
                    volumeChanger();
                     try
                     {
                         Thread.sleep(5000);
                         Log.i("Noise", "Tock");

                     } catch (InterruptedException e) { };

                     mHandler.post(updater);
                 }
             }
         };

         runner.start();
         Log.d("Noise", "start runner()");
     }
 }

 public void onResume()

 {
     super.onResume();
     startRecorder();
 }

 public void onPause()
 {
    super.onResume();
    startRecorder();
     //super.onPause();
     //super.stopRecorder();
 }

 /*@Override
 public void onBackPressed()
 {
    super.onResume();
    startRecorder();
 }*/

 public void startRecorder(){
     if (mRecorder == null)
     {
         mRecorder = new MediaRecorder();
         mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
         mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
         mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
         mRecorder.setOutputFile("/dev/null"); 
         try
         {           
             mRecorder.prepare();
         }catch (java.io.IOException ioe) {
             android.util.Log.e("[Monkey]", "IOException: " + 
 android.util.Log.getStackTraceString(ioe));

         }catch (java.lang.SecurityException e) {
             android.util.Log.e("[Monkey]", "SecurityException: " +   
 android.util.Log.getStackTraceString(e));
         }
         try
         {           
             mRecorder.start();
         }catch (java.lang.SecurityException e) {
             android.util.Log.e("[Monkey]", "SecurityException: " +    
 android.util.Log.getStackTraceString(e));
         }

         //mEMA = 0.0;
     }

 }
 /*public void stopRecorder() {
     if (mRecorder != null) {
         mRecorder.stop();       
         mRecorder.release();
         mRecorder = null;
     }
 }*/

 public void updateTv(){

    double amp=getAmplitudeEMA();
     mStatusView.setText(Double.toString((amp)) + " dB");
 }

 public double soundDb(double ampl){
     double intensity=20 * Math.log10(getAmplitudeEMA() / ampl);
    return  intensity;
 }
 public double getAmplitude() {
     if (mRecorder != null)
         return  (mRecorder.getMaxAmplitude());
     else
         return 0;

 }
 public double getAmplitudeEMA() {
     double amp =  getAmplitude();
     mEMA = EMA_FILTER * amp + (1.0 - EMA_FILTER) * mEMA;
     return mEMA;
 }

 public void volumeChanger()
 {
     volumeBar = (SeekBar) findViewById(R.id.sb_volumebar);

      //get the audio manager
      amanager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);

      //seek bar settings//
      //sets the range between 0 and the max volume
      volumeBar.setMax(amanager.getStreamMaxVolume(AudioManager.STREAM_RING));
      //set the seek bar progress to 1
      //volumeBar.setKeyProgressIncrement(1);

      //sets the progress of the seek bar based on the system's volume
     // volumeBar.setProgress(500);

      if(mEMA<(double)800.00)
      {
         volumeBar.setProgress((int)amanager.getStreamMaxVolume(AudioManager.STREAM_RING)/5);
      }

      else if(mEMA<(double)15000.00)
      {
         volumeBar.setProgress((int)amanager.getStreamMaxVolume(AudioManager.STREAM_RING)*2/5);
      }

      else if(mEMA<25000.00)
      {
         volumeBar.setProgress((int)amanager.getStreamMaxVolume(AudioManager.STREAM_RING)*3/5);
      }

      else if(mEMA<50000.00)
      {
         volumeBar.setProgress((int)amanager.getStreamMaxVolume(AudioManager.STREAM_RING)*4/5);
      }

      else
      {
         volumeBar.setProgress((int)amanager.getStreamMaxVolume(AudioManager.STREAM_RING));
      }

      //register OnSeekBarChangeListener, so that the seek bar can change the volume
        volumeBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
        {
            @Override
            public void onStopTrackingTouch(SeekBar seekBar)
            {
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar)
            {
            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
            {
                int index=volumeBar.getProgress();
                //change the volume, displaying a toast message containing the current volume and playing a feedback sound
                amanager.setStreamVolume(AudioManager.STREAM_RING, index, AudioManager.FLAG_SHOW_UI);
            }
        });
  }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        //if one of the volume keys were pressed
        if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP)
        {
            //change the seek bar progress indicator position
            volumeBar.setProgress(amanager.getStreamVolume(AudioManager.STREAM_RING));
        }

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(true);
            return true;
        }

        //propagate the key event
        return super.onKeyDown(keyCode, event);
    }

 }
4

2 回答 2

0

让测量代码始终监听服务并让它更新共享偏好。

然后所有活动需要做的就是监听共享偏好的变化并相应地更新 UI。

您显然可以使用任意数量的机制来允许活动响应服务更改,但我认为侦听共享首选项更改可能最容易开始。

listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
  public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
    //update ui if your key was updated
  }
};

prefs.registerOnSharedPreferenceChangeListener(listener);
于 2013-09-14T09:13:46.087 回答
0

我想出了另一种方法。广播接收器可用于与广播服务同步的主要活动。

于 2013-09-19T08:56:15.677 回答