【问题标题】:Setting an alarm after reboot重启后设置闹钟
【发布时间】:2014-02-09 16:05:18
【问题描述】:

我有以下课程用于在每天的特定时间设置闹钟:

public class MainActivity extends Activity {
    private AlarmManager alarmMgr;
    private PendingIntent alarmIntent;
    BroadcastReceiver br;
    TextView t; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setup(); 
        t = (TextView)findViewById(R.id.textView1);
        // Set the alarm to start at approximately 2:00 p.m.
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, 18);
        calendar.set(Calendar.MINUTE, 10); // Particular minute
        calendar.set(Calendar.SECOND, 0);
         alarmMgr = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
         alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                    1000*60*60*24, alarmIntent);
    }


    public void setup() {
        br = new BroadcastReceiver() {
            @Override
            public void onReceive(Context c, Intent i) {
                Toast.makeText(c, "Rise and Shine!", Toast.LENGTH_LONG).show();
                //Invoke the service here Put the wake lock and initiate bind service
                t.setText("Hello Alarm set");
            }
        };
        registerReceiver(br, new IntentFilter("com.testrtc") );
        alarmIntent = PendingIntent.getBroadcast( this, 0, new Intent("com.testrtc"),
                0 );
        alarmMgr = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));
    }
   }

以上工作正常,但是要在重启后再次设置警报,我使用以下内容:

public class SampleBootReceiver extends BroadcastReceiver {
    private AlarmManager alarmMgr;
    private PendingIntent alarmIntent;
    BroadcastReceiver br;
    TextView t; 

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            Toast.makeText(context, "Hello from Bootloader", 10000).show();
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.set(Calendar.HOUR_OF_DAY, 18);
            calendar.set(Calendar.MINUTE, 10); // Particular minute
            calendar.set(Calendar.SECOND, 0);
             alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
             alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                        1000*60*60*24, alarmIntent);


        }
    }
}

我得到了祝酒词,但闹钟没有重置。否则我会得到原始的吐司消息(在MainActivityonRecieve 中设置),我做得对还是有更多?

【问题讨论】:

  • 总是在重置警报之前取消警报..虽然我猜你不必在重启后重置警报..
  • 但是在重启时取消警报?生病必须听重启前发生的事情。

标签: android android-alarms


【解决方案1】:

你的代码是 100% 正确的,但问题出在下面一行,

calendar.set(Calendar.HOUR_OF_DAY, 18);

这将允许在下次执行时执行警报。这意味着明天的 18:00 PM。相反,如果你使用它,

calendar.set(Calendar.HOUR, 18);

那么它将在今天晚上 18:00 执行。

【讨论】:

  • HOUR_OF_DAY 用于 24 小时制。例如,在晚上 10:04:15.250,HOUR_OF_DAY 为 22。HOUR 用于 12 小时制。例如,在晚上 10:04:15.250,HOUR 是 10。
  • @Tobor,是的,你是对的,这是我的错误,我只是把它和这个stackoverflow.com/questions/4562757/…混淆了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多