使用AsyncTask并重新启动方法

时间:2015-06-18 17:24:08

标签: java android android-asynctask

这是我的第一个项目,所以请原谅我粗略的代码,特别是这个if()的东西;)我会稍后尝试修复它。

1问题 - 当应用启动时,它没有显示我在Activity Main布局中做了什么。当结果发送到显示时,我会显示UI。

我想我需要在某处添加AsyncTask。我尝试过一些例子,但我做错了。也许有人可以在应用程序启动后立即查看我的应用用户界面,帮助我找到我需要的代码。

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'>&#8679</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'>&#8681</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"/>

1 个答案:

答案 0 :(得分:1)

您的活动(以及您的应用)在onCreate()返回之前不会显示,这就是您没有看到任何内容的原因。你绝对应该从该函数中删除记录,选项是将其移动到asyncTask,移动到服务或简单地移动到另一个具有Runnable的线程。

AsyncTask可能是最好的选择,它也可以帮助您解决第二个问题。只需连续记录任务,将结果发送到活动并在屏幕上绘制。

永远记住,在android中,主线程应该只处理与视觉相关的任务,并且所有工作都应该移动到另一个线程。 Android提供了几个选项来帮助您实现这一目标(如asyncTask)