报警管理触发器通知 [英] Alarm Manager Triggers Notification

查看:209
本文介绍了报警管理触发器通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个报警经理触发通知每个星期天。它工作正常,但每当我打开应用程序,即使它不是星期天,显示通知。在应用程序打开时的onReceive方法所触发?我怎么可以改变它,以便应用程序打开时,通知不显示?

code:

 包com.sjjgames.abortionappnoads;

进口的java.util.Calendar;
进口android.app.AlarmManager;
进口android.app.NotificationManager;
进口android.app.PendingIntent;
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.os.PowerManager;
进口android.support.v4.app.NotificationCompat;
进口android.support.v4.app.TaskStackBuilder;
进口android.widget.Toast;

    公共类报警扩展的BroadcastReceiver
    {
         @覆盖
         公共无效的onReceive(上下文的背景下,意图意图)
         {
             电源管理器PM =(电源管理器)context.getSystemService(Context.POWER_SERVICE);
             PowerManager.WakeLock WL = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,);
             wl.acquire();

                //这里把你的code。
                //通知
                NotificationCompat.Builder mBuilder =
                        新NotificationCompat.Builder(上下文)
                        .setSmallIcon(R.drawable.icon)
                        .setContentTitle(胎儿发育)
                        .setContentText(点击这里,查看您的孩子的发展,这个星期!);
                意图resultIntent =新的意图(背景下,StatusOfChild.class);
                TaskStackBuilder stackBuilder = TaskStackBuilder.create(上下文);
                stackBuilder.addParentStack(StatusOfChild.class);
                stackBuilder.addNextIntent(resultIntent);
                PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
                mBuilder.setContentIntent(resultPendingIntent);
                NotificationManager mNotificationManager =(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
                mNotificationManager.notify(9011,mBuilder.build());

             wl.release();
         }

     公共无效SetAlarm(上下文的背景下)
     {
         AlarmManager AM =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
         意图I =新的意图(背景下,Alarm.class);
         PendingIntent圆周率= PendingIntent.getBroadcast(上下文,0,I,0);
         / *
         台历挂历= Calendar.getInstance();
         calendar.set(Calendar.DAY_OF_WEEK,1);
         calendar.set(Calendar.HOUR_OF_DAY,12);
         calendar.set(Calendar.MINUTE,00);
         calendar.set(Calendar.SECOND,00);
         * /
        漫长的一天= 0;
        日历dayCalendar = Calendar.getInstance();
        长DAYOFWEEK = dayCalendar.get(Calendar.DAY_OF_WEEK);
        如果(一周中的某天== 1){//星期日
            天= System.currentTimeMillis的();
        }
        如果(一周中的某天== 2){//周一
            天= 24 * 60 * 60 * 1000 * 2;
        }
        如果(一周中的某天== 3){//星期二
            天= 24 * 60 * 60 * 1000 * 3;
        }
        如果(一周中的某天== 4){//星期三
            天= 24 * 60 * 60 * 1000 * 4;
        }
        如果(一周中的某天== 5){//星期四
            天= 24 * 60 * 60 * 1000 * 5;
        }
        如果(一周中的某天== 6){//周五
            天= 24 * 60 * 60 * 1000 * 6;
        }
        如果(一周中的某天== 7){//星期六
            天= 24 * 60 * 60 * 1000;
        }
        am.setRepeating(AlarmManager.RTC_WAKEUP,一天,24 * 60 * 60 * 1000 * 7,PI);

     }

     公共无效CancelAlarm(上下文的背景下)
     {
         意向意图=新的意图(背景下,Alarm.class);
         PendingIntent发件人= PendingIntent.getBroadcast(上下文,0,意图,0);
         AlarmManager alarmManager =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
         alarmManager.cancel(寄件人);
     }

 }
 

解决方案

它,是因为有一个事实,即<一href="http://developer.android.com/reference/android/app/AlarmManager.html#setRepeating%28int,%20long,%20long,%20android.app.PendingIntent%29"相对=nofollow> setRepeating()将立即触发报警,如果它是在过去。您将需要计算的时间差,然后shedule报警。添加到日历似乎是一个简单的方法来做到这一点。

例如,如果你使用日历和当前的一天是星期一:

 台历挂历= Calendar.getInstance();
         calendar.set(Calendar.HOUR_OF_DAY,12);
         calendar.set(Calendar.MINUTE,00);
         calendar.set(Calendar.SECOND,00);
         calendar.add(Calendar.DATE,6); //增加6天。
 

I have an alarm manager that triggers a notification every Sunday. It works fine but whenever I open the application, the notification is displayed even if it is not Sunday. Is the onReceive method triggered when the application opens? How could I change it so the notification does not display when the app is opened?

CODE:

package com.sjjgames.abortionappnoads;

import java.util.Calendar;
import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.widget.Toast;

    public class Alarm extends BroadcastReceiver
    {
         @Override
         public void onReceive(Context context, Intent intent) 
         {   
             PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
             PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
             wl.acquire();

                // Put here YOUR code.
                //NOTIFICATION
                NotificationCompat.Builder mBuilder =
                        new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.icon)
                        .setContentTitle("Fetal Development")
                        .setContentText("Click here to view you child's development for this week!");
                Intent resultIntent = new Intent(context, StatusOfChild.class);
                TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
                stackBuilder.addParentStack(StatusOfChild.class);
                stackBuilder.addNextIntent(resultIntent);
                PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
                mBuilder.setContentIntent(resultPendingIntent);
                NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                mNotificationManager.notify(9011, mBuilder.build());

             wl.release();
         }

     public void SetAlarm(Context context)
     {
         AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
         Intent i = new Intent(context, Alarm.class);
         PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
         /*
         Calendar calendar = Calendar.getInstance();
         calendar.set(Calendar.DAY_OF_WEEK, 1);
         calendar.set(Calendar.HOUR_OF_DAY, 12);
         calendar.set(Calendar.MINUTE, 00);
         calendar.set(Calendar.SECOND, 00);
         */
        long day = 0;
        Calendar dayCalendar = Calendar.getInstance();
        long dayOfWeek = dayCalendar.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == 1){ //Sunday
            day = System.currentTimeMillis();
        }
        if (dayOfWeek == 2){ //Monday
            day = 24*60*60*1000*2;
        }
        if (dayOfWeek == 3){ //Tuesday
            day = 24*60*60*1000*3;
        }
        if (dayOfWeek == 4){ //Wednesday
            day = 24*60*60*1000*4;
        }
        if (dayOfWeek == 5){ //Thursday
            day = 24*60*60*1000*5;
        }
        if (dayOfWeek == 6){ //Friday
            day = 24*60*60*1000*6;
        }
        if (dayOfWeek == 7){ //Saturday
            day = 24*60*60*1000;
        }
        am.setRepeating(AlarmManager.RTC_WAKEUP, day, 24*60*60*1000*7, pi);

     }

     public void CancelAlarm(Context context)
     {
         Intent intent = new Intent(context, Alarm.class);
         PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
         AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
         alarmManager.cancel(sender);
     }

 }

解决方案

It has to do with the fact that setRepeating() will trigger the alarm immediately if it is in the past. You will need to compute the time difference then shedule the alarm. Adding to the calendar seems like an easy way to do it.

Eg, if you were using Calendar and the current day is Monday:

Calendar calendar = Calendar.getInstance();
         calendar.set(Calendar.HOUR_OF_DAY, 12);
         calendar.set(Calendar.MINUTE, 00);
         calendar.set(Calendar.SECOND, 00);
         calendar.add (Calendar.DATE, 6); //add 6 days.

这篇关于报警管理触发器通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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