报警器不工作在Android的? [英] Alarm not working in android?

查看:110
本文介绍了报警器不工作在Android的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法摆脱的东西,我想,是非常微不足道的。 基本上我在未来的调度报警了特定的时刻:

I get stuck with something that , I guess , is very trivial. Basically I am scheduling alarm for a given moment in the future :

Intent contentIntent = new Intent(this, AlarmReceiver.class); 
PendingIntent theappIntent = PendingIntent.getService(Main.this, 0,contentIntent, 0); 
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day, hour,minute); 
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), theappIntent); 

工作正常,而手机是不断地对。 我的问题是不会触发报警手机重新启动后, 并且在期望的时间导通。 有什么我错过了什么?我应该做一些额外的工作,以保持报警 计划启动后?

works fine while mobile is constantly on. My problem is that alarm is not triggered after mobile is restarted and is TURNED ON on the expected time. Is there something I missed? Should I do some extra work to keep alarm scheduled after restart?

推荐答案

您必须重置报警时,设备获取引导。 你缺少检测应用程序时,设备启动和复位低于alarm..so是code写在清单

You have to reset the alarm when device get boot. You are missing to detect the app when device boot and reset the alarm..so below is code written in manifest

<receiver android:name=".YourReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
</receiver>

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

此外,创建一个BroadcastReceiver收取开机完成意向

Also create a BroadcastReceiver to receive the boot completed intent

 public class YourReceiver extends BroadcastReceiver {


 Context ct;

 @Override
 public void onReceive(Context context, Intent intent) 
 {
    ct=context;
    if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
    {
      // Reset your alarm here
      Intent contentIntent = new Intent(ct, AlarmReceiver.class); 
      PendingIntent theappIntent = PendingIntent.getService(ct, 0,contentIntent, 0); 
      Calendar calendar = Calendar.getInstance();
      calendar.set(year, month, day, hour,minute); 
      AlarmManager am = (AlarmManager) ct.getSystemService(ALARM_SERVICE);
      am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), theappIntent); 
    }
 }

这篇关于报警器不工作在Android的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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