0

我是安卓新手。我正在尝试开发一个闹钟应用程序,它实际上是一个会说话的时钟。我只想让时钟使用 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)如果有人给我实现这个应用程序的想法,我将不胜感激,我相信有很多专家会通过相同的应用程序。

问候

奥迈尔

4

3 回答 3

2

这是我在闹钟应用程序中使用的一些示例代码,希望对您有所帮助。

设置闹钟:

私人无效 setAlarm(){

    Context context = getApplicationContext();

    AlarmManager mgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, OnAlarmReceiver.class);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
    myCal = Calendar.getInstance();
    myCal.setTimeInMillis(TIME_THE_ALARM_SHOULD_GO_OFF_AS_A_LONG);
    mgr.set(AlarmManager.RTC_WAKEUP, myCal.getTimeInMillis(), pi);
    Log.i(myTag, "alarm set for " + myCal.getTime().toLocaleString());
    Toast.makeText(getApplicationContext(),"Alarm set for " + myCal.getTime().toLocaleString(), Toast.LENGTH_LONG).show();

}

这在 onAlarmReceiver 类中:

公共无效onReceive(上下文上下文,意图意图){意图i =新意图(上下文,AlarmActivity.class);i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); }

这将在需要关闭时启动 AlarmActivity。在您的情况下,您会将吐司和语音放入 AlarmActivity。

于 2010-12-18T22:53:37.167 回答
1

How can I implement this app so that when the alarm is set, it can actually run as a service in the notification bar where the original AlarmClock runs. So that even if the app is closed its still running to invoke the alarm message at the right time.

Do not do this. Having a service stick around in memory 24x7 to watch a clock is a waste of RAM and will get you attacked by task killers, reducing your app's effectiveness. Please stick with AlarmManager.

I cannot show any Dialog box or can use TTS if the AlarmManager invokes a Class that extends either Service or BroadcastReciever.

Start an activity, perhaps a dialog-themed activity.

于 2010-12-18T19:56:29.320 回答
0

刚刚得到答案,无论您使用什么服务、接收器、活动等,您都需要在您的 AndroidManifest.xml 中注册它。否则它不会工作

于 2010-12-21T09:47:58.697 回答