0

任何人都可以帮助我使用用于设置时间的普通闹钟应用程序TimePicker..并且还使用意图等而不是正常比较。这是我到目前为止所做的代码。但这不起作用。“TimePicker”设置时间,按下“ToggleButton”时,“TextVew”显示警报已开启。但是当达到警报时间时,不会显示警报响铃消息。请有人帮帮我。这是主要活动的代码

public  class FullscreenActivity extends Activity implements OnClickListener  {
        TimePicker TimePickerForGettingTime;
        TextView AlarmSet;
        TextView AlarmOnOff;



        ToggleButton ConfirmButton;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fullscreen);


       TimePickerForGettingTime=   (TimePicker)findViewById(R.id.TimePickerForGettingTime);
       ConfirmButton = (ToggleButton) findViewById(R.id.ConfirmButton);




        ConfirmButton.setOnClickListener(new OnClickListener(){

               @Override
            public void onClick(View v)
            {      boolean on=((ToggleButton) v).isChecked() ;
                  AlarmOnOff=(TextView)findViewById(R.id.AlarmOnOff);

                  if(on)
                  {     
                   AlarmOnOff.setText("Alarm on");

                    Calendar cal = Calendar.getInstance();
                            cal.set(TimePickerForGettingTime.getCurrentHour(),TimePickerForGettingTime.getCurrentMinute(),00);

                        setAlarm(cal);

                  }
                  else
                  { 
                      AlarmOnOff.setText("Alarm off");
                  }
                  }

        });
     }
      private void setAlarm(Calendar targetCal)
      {


          Intent alarmintent = new Intent(FullscreenActivity.this, AlarmReceiver.class);
          PendingIntent sender = PendingIntent.getBroadcast(FullscreenActivity.this, 0, alarmintent, 0);
          AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
          alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), sender);   


        }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

 }

这是 onbootreceiver 类

   public class OnBootReceiver extends BroadcastReceiver {
      private static final int PERIOD=10000;  // 10sec

      @Override
      public void onReceive(Context context, Intent intent) {
      AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
      Intent i=new Intent(context, AlarmReceiver.class);
      PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0);

      mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), PERIOD, pi);
 }
}

这是警报接收器类

   public class AlarmReceiver extends BroadcastReceiver {

     @Override
     public void onReceive(Context context, Intent intent) {



 PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
 NotificationManager manager =    (NotificationManager)context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
    Notification noti = new Notification(android.R.drawable.stat_notify_more, "Wake up alarm", System.currentTimeMillis());
    noti.setLatestEventInfo(context, "My Alarm", "WAKE UP...!!!", sender);
    noti.flags = Notification.FLAG_AUTO_CANCEL;
    manager.notify(R.string.app_name, noti); 

    //intent to call the activity which shows on ringing
    Intent myIntent = new Intent(context, FullscreenActivity.class);
    myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(myIntent);

    //display that alarm is ringing
    Toast.makeText(context, "Alarm Ringing...!!!", Toast.LENGTH_LONG).show();
}
 }
4

1 回答 1

3

您是否在清单中注册了警报接收器?

你应该在你的清单中有这个。

 <application>
   .
   .
   .
 <receiver android:name=".AlarmReceiver" android:process=":remote" />
</application>

另外,我不确定这是设置日历的正确方法,但我可能是错的。我这样做,对我来说效果很好

            Calendar cal = Calendar.getInstance();

           cal.set (Calendar.HOUR_OF_DAY, time_picker.getCurrentHour());
           cal.set (Calendar.MINUTE, time_picker.getCurrentMinute());
于 2013-09-14T12:35:21.937 回答