Saturday, 14 September 2013

Making same android activity class as a service ondestroy

Making same android activity class as a service ondestroy

This is an application that measures the sound intensity and in accordance
with that measured value it sets the seek bar and in turn it adjusts the
ringer volume in accordance with the seek bar automatically.
I want to run this application in foreground as well as in background when
user destroys it. Because the user will definately be needing the app even
after he quits it. I have read the documentation of creating a service but
the problem is i want full code same as activity to be run in the
background after the app destroyal..So any help would be greatly
appreciated.
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);
}
}

No comments:

Post a Comment