這是我的第一個項目,所以請原諒我粗略的代碼,特別是這個if()的東西;)我會盡力修復它。使用AsyncTask並重新啓動方法
1問題 - 當應用程序啓動時,它不顯示我在Activity Main layout中做過的操作。當結果發送到顯示器時,我的UI顯示出來。
我想我需要在某處添加AsyncTask。我嘗試了一些例子,但我做錯了什麼。也許有人可以幫助我在應用啓動後立即看到我的應用UI。
2問題 - 第一次結果發送到屏幕後,如何在程序中重新啓動audioRecord任務? 目前我正在打破while循環以便能夠將文本發送到屏幕。 如果我試圖從循環中設置文本,在循環運行期間,什麼都不會發生。
在此先感謝!
MainActivity.java代碼:
import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
private static final int RECORDER_SAMPLERATE = 8000;
private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
public AudioRecord recorder = null;
int numCrossing, p, numSamples;
short audioData[];
public int frequency;
public boolean recording;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String TAG = "tulemus";
int bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,
RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING);
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
RECORDER_SAMPLERATE, RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING, bufferSize);
recorder.startRecording();
recording = true;
audioData = new short[bufferSize];
int[] values;
int k = 0, t = 0;
values = new int[2];
while (recording) {
numCrossing = 0;
numSamples = 0;
recorder.read(audioData, 0, bufferSize);
int mod = (bufferSize/4) * 4;
for (p = 0; p < mod; p += 4) {
if (audioData[p] > 0 && audioData[p + 1] <= 0) numCrossing++;
if (audioData[p] < 0 && audioData[p + 1] >= 0) numCrossing++;
if (audioData[p + 1] > 0 && audioData[p + 2] <= 0) numCrossing++;
if (audioData[p + 1] < 0 && audioData[p + 2] >= 0) numCrossing++;
if (audioData[p + 2] > 0 && audioData[p + 3] <= 0) numCrossing++;
if (audioData[p + 2] < 0 && audioData[p + 3] >= 0) numCrossing++;
if (audioData[p + 3] > 0 && audioData[p + 4] <= 0) numCrossing++;
if (audioData[p + 3] < 0 && audioData[p + 4] >= 0) numCrossing++;
numSamples += 4;
}
for (p = 0; p < bufferSize; p++) {
if (audioData[p] > 0 && audioData[p + 1] <= 0) numCrossing++;
if (audioData[p] < 0 && audioData[p + 1] >= 0) numCrossing++;
numSamples++;
}
frequency = (8000/numSamples) * numCrossing;
Log.d(TAG, String.valueOf(frequency));
if (frequency >= 2550 && frequency <= 2750 && (values[0] != 2)) {
values[0] = 1;
values[1] = 2;
k = 1;
}//if
if (frequency <= 3090 && frequency >= 2900 && (values[0] == 0)) {
values[0] = 2;
values[1] = 1;
k = 1;
}//if
if (frequency <= 3090 && frequency >= 2900 && (values[0] == 1)) {
t = 1;
break;
}//if
if (frequency >= 2550 && frequency <= 2750 && (values[0] == 2)) {
t = 2;
break;
}//if
if (k != 0) {
k = k + 1;
}//if
if (k == 20) {
values[0] = 0;
values[1] = 0;
}//if
}//while
if (t == 1) {
TextView textView = (TextView) findViewById(R.id.textView);
String next = "<font color='#FF0000'>⇧</font>";
textView.setText(Html.fromHtml(next));
values[0] = 0;
values[1] = 0;
}//if
if (t == 2) {
TextView textView = (TextView) findViewById(R.id.textView);
String next = "<font color='#FFFF00'>⇩</font>";
textView.setText(Html.fromHtml(next));
values[0] = 0;
values[1] = 0;
}//if
}//onCreate
public void onToggleClicked(View view) {
// Is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
if (on) {
} else {
System.exit(0);
}
}
}
activity_main.xml中代碼:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:clickable="false"
android:id="@+id/RelLayout"
android:focusable="false"
android:visibility="visible"
android:background="#ff000000">
<TextView
android:layout_width="200dp"
android:layout_height="1000dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:textSize="50sp" />
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/switch1"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:checked="true"
android:onClick="onToggleClicked"/>
感謝您的重播。我試圖自己弄清楚這個AsyncTask,但目前對我來說似乎太複雜了。 – AlarMa
據我所知,我可以只使用doInBackground爲我的任務,並使用UpdateProgress在循環中獲得結果,而在後臺運行?我可以從AsynkTask返回這兩個字符串「next」,並使用onProgressUpdate()中的publishProgress()和setText顯示它們嗎? – AlarMa
好吧,今天我比昨天更聰明一點;)看起來我需要使用Service,因爲我需要長時間運行後臺任務(小時),而AsynkTask並沒有這樣做,操作系統可以非常輕鬆地殺死AsynkTask。 – AlarMa