将相同的android活动类作为服务ondestroy

时间:2013-09-14 09:03:53

标签: android android-service

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

我想在前台和后台运行此应用程序  当用户销毁它时。因为用户肯定会需要  应用程序甚至在他退出之后。我已经阅读了文档  创建一个服务,但问题是我想要完整的代码相同  应用程序销毁后在后台运行的活动。所以  非常感谢帮助。

 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);
    }

 }

2 个答案:

答案 0 :(得分:0)

让测量代码始终监听服务并让它更新共享首选项。

然后,所有活动需要做的是收听共享首选项的更改并相应地更新UI。

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

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

prefs.registerOnSharedPreferenceChangeListener(listener);

答案 1 :(得分:0)

我已经找到了另一种方法。 广播接收器可以在主要活动中与广播服务同步使用。