简体   繁体   English

AlarmManager在相应时间不触发警报

[英]AlarmManager doesn't fire alarm at the corresponding time

Can you plese help me understand why my alarm won't fire : 您能否请我帮助我理解为什么我的警报不会触发:
I am trying to register a pending intent in oder to fire the alarm at 12:22 (current hour now) but the problem is that the alarm won't fire . 我正在尝试注册一个有意向的对象,以在12:22(现在是当前时间)触发警报,但是问题是警报不会触发。 I tried substituting the miliseconds from calendar.getTimeInMilies() with a hard-encoded time : 10 000 milies (10 seconds); 我尝试用硬编码的时间替换calendar.getTimeInMilies()中的毫秒:10000毫秒(10秒); Not even that it had worked . 甚至没有奏效。

    AlarmManager alarm_manager =(AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
    calendar = Calendar.getInstance();

    Intent intent = new Intent(MainActivity.this , AlarmManagerHelper.class);

    PendingIntent register =PendingIntent.getActivity(getBaseContext(), 0, intent,0);

    calendar.set(Calendar.YEAR,2014);
    calendar.set(Calendar.MONTH, 10);
    calendar.set(Calendar.DAY_OF_MONTH, 2);
    calendar.set(Calendar.HOUR_OF_DAY,12);
    calendar.set(Calendar.MINUTE,22);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.AM_PM, Calendar.AM);




    alarm_manager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), register);

    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
    Log.d(TAG ,"Time format is :" +formatter.format(calendar.getTime()));

I also have the following permission set in the Manifest 我在清单中也设置了以下权限

    android:name="android.permission.WAKE_LOCK"

And the output : 和输出:

    Time format is :02/11/2014 12:22:00

Try to use getBroadcast method instead of getActivity: 尝试使用getBroadcast方法代替getActivity:

PendingIntent register =PendingIntent.getBroadcast(MainActivity.this, 0, intent,0);

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM