设置通知提醒 [英] set notification alert

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

问题描述

喜IA新到到Android ..
我已经开发了一个程序来设置通知onspecific时间
但问题是,它显示只通知日期2011年10月前
如果我进入11日起任其没有显示任何通知..
似乎有些奇怪,但这里真的卡住....

主要活动..

 公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    日历CAL = Calendar.getInstance(); //使用这个你需要    cal.set(的Calendar.MONTH,11);
    cal.set(Calendar.YEAR,2011年);
    cal.set(Calendar.DAY_OF_MONTH,25);
    cal.set(Calendar.HOUR_OF_DAY,18);
    cal.set(Calendar.MINUTE,28);    意图alarmintent =新意图(getApplicationContext()
            AlarmReceiver.class);
    alarmintent.putExtra(称号,我们通知的标题);
    alarmintent.putExtra(注意,我们通知的说明);
    INT HELLO_ID = 1;
    发件人的PendingIntent = PendingIntent.getBroadcast(
            getApplicationContext(),HELLO_ID,alarmintent,
            PendingIntent.FLAG_UPDATE_CURRENT |
             Intent.FILL_IN_DATA);    AlarmManager AM =(AlarmManager)getSystemService(ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),发送者);
}

和报警reciever活动...

 公共类AlarmReceiver扩展广播接收器{    私有静态最后弦乐NotificationManager = NULL;
    私有静态诠释NOTIFICATION_ID = 0;    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){        (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationManager马槽=(NotificationManager)上下文
                .getSystemService(Context.NOTIFICATION_SERVICE);
        通知通知=新的通知(R.drawable.ic_launcher,
                康贝说明,System.currentTimeMillis的());
        的PendingIntent contentIntent = PendingIntent.getActivity(背景下,
                NOTIFICATION_ID,新的意图(背景下,
                  AlarmReceiver.class),0);
        捆绑额外= intent.getExtras();
        字符串title = extras.getString(标题);        字符串注= extras.getString(情况说明);
        notification.setLatestEventInfo(上下文,注意,标题,contentIntent);
        notification.flags = Notification.FLAG_INSISTENT;
        notification.defaults | = Notification.DEFAULT_SOUND;    manger.notify(NOTIFICATION_ID,通知);    }
};


解决方案

您正在为2011/5/5报警,此日期已过,因此立即执行。

当您收到日历的情况下,我认为,该系统当前日期和放大器自动填充;时间。因此,改变的你需要改变什么(即,如果你想在5天之发出通知,更改日的日历中的实例)

顺便说一句,再次单击通知它会的时候,打电话给你的广播接收器这个时候的意图的不会有临时演员你已经把它而在活动。当单击通知,您应该推出一个活动 - 不是广播接收机

hi ia m new to android.. i have developed a programme to set notification onspecific time but the problem is it shows notification only of date before october 2011 if i enter any date of november its not showing any notification.. seems strange but really stuck here....

main activity..

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Calendar cal = Calendar.getInstance(); // for using this you need to

    cal.set(Calendar.MONTH, 11);
    cal.set(Calendar.YEAR, 2011);
    cal.set(Calendar.DAY_OF_MONTH, 25);
    cal.set(Calendar.HOUR_OF_DAY, 18);
    cal.set(Calendar.MINUTE, 28);

    Intent alarmintent = new Intent(getApplicationContext(),
            AlarmReceiver.class);
    alarmintent.putExtra("title", "Title of our Notification");
    alarmintent.putExtra("note", "Description of our  Notification");


    int HELLO_ID = 1;
    PendingIntent sender = PendingIntent.getBroadcast(
            getApplicationContext(), HELLO_ID, alarmintent,
            PendingIntent.FLAG_UPDATE_CURRENT | 
             Intent.FILL_IN_DATA);

    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
}

and the alarm reciever activity...

public class AlarmReceiver extends BroadcastReceiver {

    private static final String NotificationManager = null;
    private static int NOTIFICATION_ID = 0;

    @Override
    public void onReceive(Context context, Intent intent) {

        (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationManager manger = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.ic_launcher,
                "Combi Note", System.currentTimeMillis());
        PendingIntent contentIntent = PendingIntent.getActivity(context,
                NOTIFICATION_ID, new Intent(context, 
                  AlarmReceiver.class), 0);
        Bundle extras = intent.getExtras();
        String title = extras.getString("title");

        String note = extras.getString("note");
        notification.setLatestEventInfo(context, note, title, contentIntent);
        notification.flags = Notification.FLAG_INSISTENT;
        notification.defaults |= Notification.DEFAULT_SOUND;

    manger.notify(NOTIFICATION_ID, notification);

    }
};

解决方案

You are setting an alarm for 5/5/2011, this date has passed so it is executed immediately.

When you receive a instance of Calendar, I think that the system automatically fills in the current date & time. So change only what you need to change (I.E if you want to send a notification in 5 days, change the day only in the calendar instance)

By the way, when clicking the notification it will, again, call your BroadcastReceiver and this time the intent won't have the extras you have put in it while in the activity. When clicking the notification, you should launch an activity - not a broadcast receiver.

这篇关于设置通知提醒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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