本文介绍了安卓:待定意图不calledon贪睡多个报警的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作在我现有的应用程序添加多个报警功能。此前,报警只是单,每一件事情的罚款。我已经能够设置多个闹钟,它工作正常过,但问题是与贪睡功能。此前,贪睡正在完善。但是,多个闹钟实施后它不工作。我从来没有得到对话框打盹报警,有一次我preSS打盹按钮。这是我的code:

设置报警方式

 公共无效setAlarm(布尔AlarmEnabled,诠释AlarmHour,诠释AlarmMin){
        //构建意图/挂起意图设定报警
        意图AlarmIntent =新的意图(MultipleAlarmActivity.this,AlarmReceiver.class);
        AlarmManager AlmMgr =(AlarmManager)getSystemService(ALARM_SERVICE);
        INT _id =(INT)System.currentTimeMillis的();
        AlarmIntent.putExtra(请求code,_id);
        Log.v(MultipleAlarmActivity请求code,+ _id);
        PendingIntent发件人= PendingIntent.getBroadcast(MultipleAlarmActivity.this,_id,AlarmIntent,0);

        //构建日历对象设置报警
        日历curCalendar = Calendar.getInstance();
        日历alarmCalendar = Calendar.getInstance();

        //初始化秒和毫秒为0两个日历
        curCalendar.set(Calendar.SECOND,0);
        curCalendar.set(Calendar.MILLISECOND,0);
        alarmCalendar.set(Calendar.SECOND,0);
        alarmCalendar.set(Calendar.MILLISECOND,0);

        //更新alarmCalendar与报警小时和分钟设置
        alarmCalendar.set(Calendar.HOUR_OF_DAY,AlarmHour);
        alarmCalendar.set(Calendar.MINUTE,AlarmMin);

        //如果报警时间是现在还是过去,从时间设置为明天提前24小时选定
        如果(alarmCalendar.getTimeInMillis()&其中; = curCalendar.getTimeInMillis()){
            alarmCalendar.add(Calendar.HOUR,24);
        }
        //设置闹钟
        AlmMgr.set(AlarmManager.RTC_WAKEUP,alarmCalendar.getTimeInMillis(),发件人);

        //生成字符串通过吐司显示报警时间
        字符串CalendarHourStr;
        如果(AlarmHour> 12){
            CalendarHourStr = Integer.toString(AlarmHour  -  12);
        } 其他 {
            CalendarHourStr = Integer.toString(AlarmHour);
        }
        字符串CalendarMinStr = Integer.toString(AlarmMin);
        如果(AlarmMin小于10){
            CalendarMinStr =0+ CalendarMinStr;
        }

        字符串strAmPM;
        如果(AlarmHour&所述; 12){
            strAmPM =AM;
        }
        其他 {
            strAmPM =下午;
        }
        串alarmTime = Integer.toString(alarmCalendar.get(Calendar.MONTH)+ 1)+/+ Integer.toString(alarmCalendar.get(Calendar.DAY_OF_MONTH))+/+ Integer.toString(alarmCalendar.get(日历.YEAR))++ CalendarHourStr +:+ CalendarMinStr ++ strAmPM;
        Toast.makeText(这一点,报警设置+ alarmTime,Toast.LENGTH_LONG).show();
         Log.v(MultipleAlarmActivity setAlarm,如果中环);
    Log.v(MultipleAlarmActivity setAlarm,setAlarm称为);
}
 

报警接收机

 公共类AlarmReceiver扩展的BroadcastReceiver
{
公共静态最后弦乐ALARM_ALERT_ACTION =com.android.alarmclock.ALARM_ALERT;
公共静态最后弦乐ALARM_INTENT_EXTRA =intent.extra.alarm;

@覆盖
公共无效的onReceive(上下文的背景下,意图意图)
{
    意图=新的意图(Intent.ACTION_MAIN);
    in.setClass(背景下,SnoozeActivity.class);
    Log.v(AlarmReceiver请求code,+ intent.getIntExtra(请求code,0));
    in.putExtra(请求code,intent.getIntExtra(请求code,0));
    in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(在);
}
}
 

的打盹报警方式

 私人无效打盹()
{
        对于贪睡报警//设置日历值
        台历挂历= Calendar.getInstance();
        // INT snoozeTime = mMinute + SNOOZE_MIN;
        calendar.add(Calendar.MINUTE,SNOOZE_MIN); // SNOOZE_MIN = 1;
        长snoozeTime = calendar.getTimeInMillis();
        //构造意图和正在申请的意图设置贪睡报警
        意图AlarmIntent =新的意图(SnoozeActivity.this,AlarmReceiver.class);
        AlarmManager AlmMgr =(AlarmManager)getSystemService(ALARM_SERVICE);
        AlarmIntent.putExtra(请求code,req_ code);
        PendingIntent发件人= PendingIntent.getBroadcast(SnoozeActivity.this,req_ code,AlarmIntent,PendingIntent.FLAG_UPDATE_CURRENT);
        AlmMgr.set(AlarmManager.RTC_WAKEUP,snoozeTime,发件人);
        timer.cancel();
}
 

解决方案

我发现了这个问题。我使用的是布尔值,检查是否报警已设置与否。而且,我忘了设置这个值是当我打电话setAlarm()。那是我的错误。

I am working on adding multiple alarm functionality in my existing app. Earlier, the alarm was single only and every thing worked fine. I have been able to set multiple alarms, it is working fine too, but problem is with snooze feature. Earlier, snooze was working perfect. But, after multiple alarm implementation it is not working. I never get dialog for snoozing alarm, once I press snooze button. Here is my code:

Set alarm method

public void setAlarm(boolean AlarmEnabled, int AlarmHour, int AlarmMin) {
        //Build Intent/Pending Intent for setting the alarm
        Intent AlarmIntent = new Intent(MultipleAlarmActivity.this, AlarmReceiver.class);
        AlarmManager AlmMgr = (AlarmManager)getSystemService(ALARM_SERVICE);
        int _id = (int)System.currentTimeMillis();
        AlarmIntent.putExtra("REQUEST CODE", _id);
        Log.v("MultipleAlarmActivity RequestCode", ""+_id);
        PendingIntent Sender = PendingIntent.getBroadcast(MultipleAlarmActivity.this, _id, AlarmIntent, 0);

        //Build Calendar objects for setting alarm
        Calendar curCalendar = Calendar.getInstance();
        Calendar alarmCalendar = Calendar.getInstance();

        //Initialize Seconds and Milliseconds to 0 for both calendars
        curCalendar.set(Calendar.SECOND, 0);
        curCalendar.set(Calendar.MILLISECOND, 0);
        alarmCalendar.set(Calendar.SECOND, 0);
        alarmCalendar.set(Calendar.MILLISECOND, 0);

        //Update alarmCalendar with Alarm Hour and Minute Settings
        alarmCalendar.set(Calendar.HOUR_OF_DAY, AlarmHour);
        alarmCalendar.set(Calendar.MINUTE, AlarmMin);

        //If Alarm Time is now or in the past, set it for tomorrow 24 hours in advance from time selected
        if (alarmCalendar.getTimeInMillis() <= curCalendar.getTimeInMillis()) {
            alarmCalendar.add(Calendar.HOUR, 24);
        }
        //Set the alarm
        AlmMgr.set(AlarmManager.RTC_WAKEUP, alarmCalendar.getTimeInMillis(), Sender);

        //Build the Strings for displaying the alarm time through Toast
        String CalendarHourStr;
        if (AlarmHour > 12) {
            CalendarHourStr = Integer.toString(AlarmHour - 12);
        } else {
            CalendarHourStr = Integer.toString(AlarmHour);
        }
        String CalendarMinStr = Integer.toString(AlarmMin);
        if (AlarmMin < 10) {
            CalendarMinStr = "0" + CalendarMinStr;
        }

        String strAmPM;
        if (AlarmHour < 12) {
            strAmPM = "AM";
        }
        else {
            strAmPM = "PM";
        }
        String alarmTime = Integer.toString(alarmCalendar.get(Calendar.MONTH) + 1) + "/" + Integer.toString(alarmCalendar.get(Calendar.DAY_OF_MONTH)) + "/" + Integer.toString(alarmCalendar.get(Calendar.YEAR)) + " " + CalendarHourStr + ":" + CalendarMinStr + " " + strAmPM;
        Toast.makeText(this, "Alarm Set For " + alarmTime, Toast.LENGTH_LONG).show();
         Log.v("MultipleAlarmActivity setAlarm", "in if loop");
    Log.v("MultipleAlarmActivity setAlarm", "setAlarm called");
}

Alarm Receiver

public class AlarmReceiver extends BroadcastReceiver
{
public static final String ALARM_ALERT_ACTION = "com.android.alarmclock.ALARM_ALERT";
public static final String ALARM_INTENT_EXTRA = "intent.extra.alarm";

@Override
public void onReceive(Context context, Intent intent)
{
    Intent in = new Intent(Intent.ACTION_MAIN);
    in.setClass(context, SnoozeActivity.class);
    Log.v("AlarmReceiver RequestCode", ""+intent.getIntExtra("REQUEST CODE", 0));
    in.putExtra("REQUEST CODE", intent.getIntExtra("REQUEST CODE", 0));
    in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(in);
}
}

Method for snoozing alarm

private void snooze()
{
        //Set Calendar Value for Snooze Alarm
        Calendar calendar = Calendar.getInstance();
        //int snoozeTime = mMinute + SNOOZE_MIN;
        calendar.add(Calendar.MINUTE, SNOOZE_MIN); //SNOOZE_MIN = 1;
        long snoozeTime = calendar.getTimeInMillis();
        //Build Intent and Pending Intent to Set Snooze Alarm
        Intent AlarmIntent = new Intent(SnoozeActivity.this, AlarmReceiver.class);
        AlarmManager AlmMgr = (AlarmManager)getSystemService(ALARM_SERVICE);
        AlarmIntent.putExtra("REQUEST CODE", req_code);
        PendingIntent Sender = PendingIntent.getBroadcast(SnoozeActivity.this, req_code, AlarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlmMgr.set(AlarmManager.RTC_WAKEUP, snoozeTime, Sender);
        timer.cancel();
}
解决方案

I found out the problem. I was using a boolean value for checking whether alarm has been set or not. And, I forgot to set this value as true when I called setAlarm(). Thats my mistake.

这篇关于安卓:待定意图不calledon贪睡多个报警的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 13:54