我是安卓新手。我正在尝试开发一个闹钟应用程序,它实际上是一个会说话的时钟。我只想让时钟使用 TextToSpeech API,并在闹钟时间一到时说出问候语和当前时间。演讲部分完成。现在我想实现警报功能。但最初我只是想在 10 秒后显示祝酒词,以检查我的课程是否正常工作。而且我没有得到想要的回应,我不知道为什么?以下是课程
主类 aClockActivity
public class aClockActivity extends Activity {
/** Called when the activity is first created. */
private PendingIntent mAlarmSender;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button)findViewById(R.id.buttonOn);
button1.setOnClickListener(mStartAlarmListener);
Button button2 = (Button)findViewById(R.id.buttonOff);
button2.setOnClickListener(mStopAlarmListener);
}
private OnClickListener mStartAlarmListener = new OnClickListener() {
public void onClick(View v) {
// We want the alarm to go off 30 seconds from now.
//long firstTime = SystemClock.elapsedRealtime();
EditText Ehour = (EditText) findViewById(R.id.hour);
EditText Eminute = (EditText) findViewById(R.id.minute);
CharSequence CharHour = Ehour.getText();
CharSequence CharMinute = Eminute.getText();
int hour = Integer.parseInt(CharHour.toString());
int minute = Integer.parseInt(CharMinute.toString());
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
// calendar.add(Calendar.MINUTE, 1);
cal.add(Calendar.SECOND, 10);
mAlarmSender = PendingIntent.getBroadcast(aClockActivity.this,
0, new Intent(aClockActivity.this, Alarm_Broadcast.class), 0);
// Schedule the alarm!
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), mAlarmSender);
// Tell the user about what we did.
Toast.makeText(aClockActivity.this, "The Alarm is Set",
Toast.LENGTH_LONG).show();
}
};
private OnClickListener mStopAlarmListener = new OnClickListener() {
public void onClick(View v) {
// And cancel the alarm.
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.cancel(mAlarmSender);
// Tell the user about what we did.
Toast.makeText(aClockActivity.this, "Setting off the alarm",
Toast.LENGTH_LONG).show();
}
};
二级报警_广播
public class Alarm_Broadcast extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm worked", Toast.LENGTH_LONG).show();
}
}
注意:忽略 OnClick() 方法中的 Edittext 部分,我稍后会使用它。
除了上述问题,我想问几个问题。
1)如何实现这个应用程序,以便在设置闹钟时,它实际上可以作为服务在原始闹钟运行的通知栏中运行。因此,即使应用程序关闭,它仍会运行以在正确的时间调用警报消息。
2) 如果 AlarmManager 调用扩展 Service 或 BroadcastReciever 的类,我无法显示任何对话框或可以使用 TTS。
3)如果有人给我实现这个应用程序的想法,我将不胜感激,我相信有很多专家会通过相同的应用程序。
问候
奥迈尔