2

I found a problem when rotate a screen on android application.

My android application is to display an ECG(electrocardiogram) signal in real-time by receiving a signal via audio jack and then store in buffer. Although it's work, when i rotate a screen, the signal is lost from screen but the application did not close. How can i solve this problem?

public class MainActivity extends Activity {

ChartView lineChart;
private final Handler mHandler = new Handler();
private Runnable mTimer1;

private final Handler mHandler2 = new Handler();
private Runnable mTimer2;

private int CHART_LEN = 20000;
private int SLIDE_LEN = 1;
Queue<String> dataQueue = new LinkedList<String>();
Queue<String> chartDataQueue = new LinkedList<String>(); 
float points[] = new float[CHART_LEN];

// Properties (MIC)
public AudioRecord audioRecord;
public int mSamplesRead;                                    
public int recordingState;
public int buffersizebytes;
public int channelConfiguration = AudioFormat.CHANNEL_IN_MONO;
public int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
public static short[] buffer; // +-32767
public static final int SAMPPERSEC = 8000; // 11025             

final int BLOCK_SIZE = 1470*14;
short[] audioShortBuffer = new short[BLOCK_SIZE];
int[] projection = new int[BLOCK_SIZE];

float a[] = new float[CHART_LEN + 1];
float b[] = new float[CHART_LEN + 1];
float h[] = new float[CHART_LEN + 1];
float c[] = new float[CHART_LEN/20 + 1];

int f = 800;
float[] y = new float[CHART_LEN];
float[] e = new float[CHART_LEN];

float[] x = new float[CHART_LEN];

double factor = (2* Math.PI * f)/SAMPPERSEC;

float u = (float)0.0046;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lineChart = (ChartView) findViewById(R.id.lineChartView1);
    audioRecord = new AudioRecord(android.media.MediaRecorder.AudioSource.MIC, SAMPPERSEC, channelConfiguration, audioEncoding, 16*BLOCK_SIZE);
    audioRecord.startRecording(); 

    a[0] = 1;
    b[0] = 1;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onResume() {
    super.onResume();

    mTimer1 = new Runnable() {

        @Override
        public void run() {
            setChartData1();
            mHandler.postDelayed(this, 30);                 
        }
    };
    mHandler.postDelayed(mTimer1, 30);

    mTimer2 = new Runnable() {

        @Override
        public void run() {

            try {
                audioRecord.read(audioShortBuffer, 0, BLOCK_SIZE);
                for(int i  = 0; i < audioShortBuffer.length; i++){
                    dataQueue.add(String.valueOf(audioShortBuffer[i]));
                }

            } 
            catch (Exception e) {

            }
            mHandler2.postDelayed(this, 10);                
        }
    };
    mHandler2.postDelayed(mTimer2, 10);
}

private void setChartData1() {  

    if (dataQueue.size() >= CHART_LEN){
        if (chartDataQueue.size() >= CHART_LEN){
            for (int i = 0; i < SLIDE_LEN ; i++){
                chartDataQueue.remove();
                chartDataQueue.add(dataQueue.poll());
            }
        }
        else {
            for (int i = 0; i < CHART_LEN; i++){
                chartDataQueue.add(dataQueue.poll());
            }
        }

        for(int j = 0; j < CHART_LEN; j++) {
            points[j] = Float.valueOf(chartDataQueue.poll());      
        }

        // initial variable
        int bufferLen = points.length;
        float lastMin = points[0];
        float offsetValue = 0;

        // find minimum value
        for (int k = 1; k < bufferLen; k++){
            if (points[k] < lastMin){
                lastMin = points[k];
            }
        }
        // check value
        if (lastMin < 0){
            offsetValue = lastMin * (-1);
        }
        else {
            offsetValue = lastMin;
        }

        int n = 0;

        for (int k = 0; k < bufferLen; k++){

            if(k == n * 20){
                c[n] = h[k];
                c[n] = (float)(c[n] * 10) + offsetValue;
                n++;
            }

        }

        a[0] = a[bufferLen];
    b[0] = b[bufferLen]; 

        lineChart.setChartData(c);
        }  

      }
   }

2 Answers2

7

In your manifest > activity tag add:

<activity
    android:name="com.activity.name"
    android:configChanges="orientation|screenSize"

This will prevent your activity from getting recreated when you rotate or change your orientation, and hence you wont lose your data (will retain your previous value)

ZealDeveloper
  • 783
  • 5
  • 21
0

You can make two different layouts for landscape and portrait orientations.

Try this: http://mdaslam.wordpress.com/2013/01/15/android-programming-screen-orientationportrait-landscape/

I hope it helped.