我在Android应用程序上旋转屏幕时发现了一个问题。
我的Android应用程序是通过音频插孔接收信号然后存储在缓冲区中来实时显示ECG(心电图)信号。虽然它有效,但当我旋转屏幕时,信号会从屏幕上丢失,但应用程序没有关闭。我该如何解决这个问题?
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);
}
}
}
答案 0 :(得分:6)
在你的清单&gt;活动标签添加:
<activity
android:name="com.activity.name"
android:configChanges="orientation|screenSize"
这会阻止您在旋转或更改方向时重新创建活动,因此您不会丢失数据(将保留以前的值)
答案 1 :(得分:0)
您可以为横向和纵向方向制作两种不同的布局。
试试这个:http://mdaslam.wordpress.com/2013/01/15/android-programming-screen-orientationportrait-landscape/
我希望它有所帮助。