本文介绍了如何,如果移动是关闭启动一个报警的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我使用 AlarmManager 类设置报警。要触发报警的手机重新启动我已经使用后,广播接收器。一切工作正常,我的报警定期间隔触发。现在的问题在这种情况下就出现了:

假如我现在的时间下午2点30分我和我的设置位于下午2时35分报警。从那以后,我关掉手机。当我打开我的手机一小时后,没有报警弹出作为其上设置报警时间。这是发生的,因为当前的时间超过对我设定的闹钟时间。为了解决这个问题,我该怎么办。我已经张贴了我的code在 AlarmManager 类设置警报。请帮我解决了这一点。

 公共类AlarmReceiver扩展广播接收器{
    @燮pressWarnings(静态访问)
    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        如果(intent.getAction()。等于(android.intent.action.BOOT_COMPLETED)){
            意图myIntent =新意图(背景下,MyAlarmService.class);
            的PendingIntent的PendingIntent = PendingIntent.getService(背景下,我,myIntent,I);
            AlarmManager alarmManager =(AlarmManager)context.getSystemService(context.ALARM_SERVICE);
            台历挂历= Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis的());
            calendar.add(Calendar.MILLISECOND,(int)的Utilities.diff(NoteManager.getSingletonObject()getAlarmTime(ⅰ))。);
            alarmManager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),的PendingIntent);
        }
    }
}公共静态长差异(日期为准){
    长差= 0;
    尝试{
        //将当前时间
        日历C = Calendar.getInstance();
        差= date.getTime() - c.getTimeInMillis();
        如果(差℃,){
            //如果差为-1 - 表示报警时间是previous时间,然后电流
            //那么首先将其更改为+正面和减去形式86400000得到确切的新的时间去玩报警
            // 86400000,总没有24小时日毫秒
            差异=差* -1;
            差值= 86400000 - 差异;
        }
    }
    赶上(例外五){
        e.printStackTrace();
    }
    回差;
}

在manifest文件

 <接收机器人:名字=。AlarmReciever>
    &所述;意图滤光器>
        <作用机器人:名字=android.intent.action.BOOT_COMPLETED/>
    &所述; /意图滤光器>
< /接收器>
<使用许可权的android:NAME =android.permission.RECEIVE_BOOT_COMPLETED/>


解决方案

在Android报警应用程序做同样的,如果你的手机已关机,并有报警响了起来,这将让你的手机开机,环报警并再次进入睡眠状态。

下面是报警源链接应用你可以下载它,看看它是如何这样做。

如果你正在做别的事情在你的报警reciever然后敲响了。你基本上可以alarmreciever重新设置在手机启动,这里是一个答案,如果手机关机这可以帮助您报警问题

编辑: - 一个环节被打破,取而代之的是

In My app I use the AlarmManager class to set alarm. To trigger the alarm after the mobile is rebooted I have used BroadcastReceiver. All works fine and my alarm is triggered at regular interval. Now the problem arises in this case :

Suppose my current time is 2:30 pm and I the set my alarm at 2:35 pm. After that I switch off the mobile. After an hour when I switch on my mobile, no alarm is pop-up as the time on which the alarm is set. This is happen because the current time exceed the time on which I set the alarm. To solve this issue what should I do. I have posted my code for setting alarm in AlarmManager class. Please help me to solve this out

public class AlarmReceiver extends BroadcastReceiver {
    @SuppressWarnings("static-access")
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            Intent myIntent = new Intent(context, MyAlarmService.class); 
            PendingIntent pendingIntent = PendingIntent.getService(context, i, myIntent, i);                            
            AlarmManager alarmManager = (AlarmManager)context.getSystemService(context.ALARM_SERVICE);
            Calendar calendar = Calendar.getInstance(); 
            calendar.setTimeInMillis(System.currentTimeMillis()); 
            calendar.add(Calendar.MILLISECOND, (int) Utilities.diff(NoteManager.getSingletonObject().getAlarmTime(i)));                 
            alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        }
    }
}

public static long diff(Date date) {
    long difference = 0;
    try {
        // set current time
        Calendar c = Calendar.getInstance();
        difference = date.getTime() - c.getTimeInMillis();
        if (difference < 0) {
            // if difference is -1 - means alarm time is of previous time then current
            // then firstly change it to +positive and subtract form 86400000 to get exact new time to play alarm
            // 86400000-Total no of milliseconds of 24hr Day
            difference = difference * -1;
            difference = 86400000 - difference; 
        }
    } 
    catch (Exception e) {
        e.printStackTrace();
    }
    return difference;
}

In The Manifest File

<receiver android:name=".AlarmReciever">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

The Alarm app in the Android does the same, if your phone is switched off and there is Alarm to ring up, It will make your phone switch On , ring the alarm and go to sleep again.

Here is the link of source of Alarm app Git_Alarm app you can download it and see how it is doing this.

and if you are doing something else in your alarm reciever then to ring Alarm up. you can basically set alarmreciever again in the phone Boot up, here is the one answer which may help you Alarm problem if phone is switched off

Edit :- one link was broken, replaced it

这篇关于如何,如果移动是关闭启动一个报警的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 18:57