这是我的代码,我想在没有按钮的情况下运行它
请帮助我;
我对android有兴趣,这是我最后的学校项目
感谢参加
我的代码
package com.andronoise;
import android.app.Activity;
import android.content.SharedPreferences;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.ToggleButton;
import java.text.DecimalFormat;
public class MainActivity extends Activity implements
InputListener {
InputSuara micInput;
TextView mdBTextView;
TextView mdBFractionTextView;
LevelBar mBarLevel;
double mOffsetdB = 10;
double mGain = 2500.0 / Math.pow(10.0, 90.0 / 20.0);
double mRmsSmoothed;
double mAlpha = 0.9;
private int mSampleRate;
private int mAudioSource;
private volatile boolean mDrawing;
private volatile int mDrawingCollided;
private static final String TAG = "MainActivity";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
micInput = new InputSuara(this);
setContentView(R.layout.activity_main);
mBarLevel = (LevelBar)findViewById(R.id.bar_level_drawable_view);
mdBTextView = (TextView)findViewById(R.id.dBTextView);
mdBFractionTextView = (TextView)findViewById(R.id.dBFractionTextView);
final ToggleButton onOffButton=(ToggleButton)findViewById(
R.id.on_off_toggle_button);
ToggleButton.OnClickListener tbListener =
new ToggleButton.OnClickListener() {
@Override
public void onClick(View v) {
if (onOffButton.isChecked()) {
readPreferences();
micInput.setSampleRate(mSampleRate);
micInput.setAudioSource(mAudioSource);
micInput.start();
} else {
micInput.stop();
}
}
};
onOffButton.setOnClickListener(tbListener);
}
private void readPreferences() {
SharedPreferences preferences = getSharedPreferences("LevelMeter",
MODE_PRIVATE);
mSampleRate = preferences.getInt("SampleRate", 8000);
mAudioSource = preferences.getInt("AudioSource",
MediaRecorder.AudioSource.VOICE_RECOGNITION);
}
@Override
public void processAudioFrame(short[] audioFrame) {
if (!mDrawing) {
mDrawing = true;
double rms = 0;
for (int i = 0; i < audioFrame.length; i++) {
rms += audioFrame[i]*audioFrame[i];
}
rms = Math.sqrt(rms/audioFrame.length);
mRmsSmoothed = mRmsSmoothed * mAlpha + (1 - mAlpha) * rms;
final double rmsdB = 20.0 * Math.log10(mGain * mRmsSmoothed);
mBarLevel.post(new Runnable() {
@Override
public void run() {
mBarLevel.setLevel((mOffsetdB + rmsdB) / 100);
DecimalFormat df = new DecimalFormat("##");
mdBTextView.setText(df.format(20 + rmsdB));
int one_decimal = (int) (Math.round(Math.abs(rmsdB * 10))) % 10;
mdBFractionTextView.setText(Integer.toString(one_decimal));
mDrawing = false;
}
});
} else {
mDrawingCollided++;
Log.v(TAG, "Level bar update collision, i.e. update took longer " +
"than 20ms. Collision count" + Double.toString(mDrawingCollided));
}
}
}
答案 0 :(得分:0)
每当用户点击任何地方时,您都希望执行一些代码。
好的......我想我终于明白你在说什么了。每次用户点击任何地方时,系统都会调用此方法:
@Override
public void onUserInteraction() {
super.onUserInteraction();
}
所以在你的情况下,代码应该是:
@Override
public void onUserInteraction() {
super.onUserInteraction();
if (onOffButton.isChecked()) {
readPreferences();
micInput.setSampleRate(mSampleRate);
micInput.setAudioSource(mAudioSource);
micInput.start();
} else {
micInput.stop();
}
编辑:
来自文档:
我不确定何时需要执行代码,但请查看图表,找到合适的位置。这是一个例子:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (onOffButton.isChecked()) {
readPreferences();
micInput.setSampleRate(mSampleRate);
micInput.setAudioSource(mAudioSource);
micInput.start();
} else {
micInput.stop();
}
答案 1 :(得分:0)
喜欢这个
git://gcc.gnu.org/git/gcc.git