Android新手。
我正在尝试创建一个应用程序,每隔一段时间发出一声蜂鸣声。屏幕上有一个简单的播放按钮,它调用readyToBeep函数,该函数调用Beep.class。我没有得到错误,它编译并运行。但是我听不到MP3的声音。调用的活动ID和布局更改为buzz.xml,但我不确定我哪里出错,因为我没有收到任何错误消息。有什么迹象表明问题是什么?
TIA
MainActivity.java
package com.example.timer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void readyToBeep(View v) {
Intent intent = new Intent(this, Beep.class);
startActivity(intent);
}
@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;
}
}
Beep.java
package com.example.timer;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
public class Beep extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.buzz);
new Beep();
}
private Context Context;
Timer timer;
public Beep(){};
public Beep(Context Context) {
this.Context = Context;
timer = new Timer();
timer.schedule(new RemindTask(),
1500, //initial delay
1*1500); //subsequent rate
}
class RemindTask extends TimerTask {
public void run() {
MediaPlayer mPlayer = MediaPlayer.create(Context, R.raw.beep);
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.start();
}
}
}
现在正在使用Beep.java(感谢MagicCode)
package com.example.timer;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
public class Beep extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.buzz);
timer = new Timer();
timer.scheduleAtFixedRate(new RemindTask(),
1500, //initial delay
1*1500); //subsequent rate
}
Timer timer;
public Beep(){};
class RemindTask extends TimerTask {
public void run() {
MediaPlayer mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.beep);
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.start();
}
}
}
答案 0 :(得分:0)
这永远不会运行
public final void main() {
setContentView(R.layout.buzz);
new Beep();
}
这是Android,因此您需要覆盖onCreate()
,就像在第一个Activity
中一样。尝试
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.buzz);
new Beep();
}