使用时间选择器选择时间和意图的android警报应用程序 [英] android alarm app using timepicker to select time and intents

查看:91
本文介绍了使用时间选择器选择时间和意图的android警报应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助我使用普通的闹钟应用程序,该应用程序使用 TimePicker 设置时间..,并且还使用intent等代替常规比较。这是代码直到现在我都做过。但这是行不通的。 时间选择器设置时间,并在按下切换按钮时显示 TextVew,表示警报已打开。但是,当达到警报时间时,不会显示警报铃声消息。请有人帮帮我。
这是主要活动的代码

can anyone help me with a normal alarm clock app that uses TimePicker to set the time ..and also uses intents,etc instead of normal comparisons.This is the code that i've done till now.But this is not working. The 'TimePicker' sets the time and on pressing the 'ToggleButton' a 'TextVew' shows that alarm is on .But when the alarmtime is reached,Alarm Ringing message is not shown.Please someone help me out. this is the code of main activity

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类

this is the onbootreceiver class

   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();
}
 }


推荐答案

有您在清单中注册了警报接收器吗?

Have you registered the alarm receiver in you manifest ?

您应该在清单中包含此内容。

You should have this in your manifest.

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

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

Also, I am not sure that is the right way to calendar set, but I could be wrong . I do it like this and it works fine for me

            Calendar cal = Calendar.getInstance();

           cal.set (Calendar.HOUR_OF_DAY, time_picker.getCurrentHour());
           cal.set (Calendar.MINUTE, time_picker.getCurrentMinute());

这篇关于使用时间选择器选择时间和意图的android警报应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆