我需要在我的android应用中实现Notification

我需要在我的android应用中实现Notification

本文介绍了我需要在我的android应用中实现Notification Reminder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求:我希望我的应用程序在每个星期五@上午8点显示提醒通知

My Requirment: I want my application to show a reminder notification on every Friday @ 8 am

我已经使用AlarmManager,BroadcastReceivers来实施我的提醒通知。我遇到的问题是,当我使用当前系统时间设置通知时间并在其中添加2分钟时...当我以这种方式使用它时,它工作得很好,它恰好在2分钟后触发了我的通知。

I have used AlarmManager, BroadcastReceivers to implement my reminder notification. The problem I am getting is, When I set notification time using current system time and Add 2 minutes in it... It works perfectly when I use it in this way, it trigger my notification exactly after 2 minutes.

但是,

当我使用日历实例在每天的特定时间设置通知时间时,它将在任何时候触发我的提醒通知我在设备/模拟器上启动/打开我的应用程序,其次它不会在指定时间触发通知

When I use calender instance to set notification time at specific time of any day, it trigger my reminder notification when ever I start/open my app on my device/emulator and secondly it wont trigger notification at specified time

以下是我的课程

这是我的HomeActivity.java

Here is my HomeActivity.java

// long when = System.currentTimeMillis()+2*60*1000;         // notification time
// WHEN I RUN THE ABOVE COMMENTED CODE… THE REMINDER IS TRIGGERD AFTER EXACTLY 2 MINS
//BUT WHEN I USE THE BELOW CODE USING CALENDER INSTANCE, IT TRIGGER MY REMINDER    IMMIDIETLY WHEN I RUN IT ON MY DEVICE/EMULATOR


    Calendar calendar =  Calendar.getInstance();
    //calendar.set(2014,Calendar.getInstance().get(Calendar.MONTH),Calendar.SUNDAY , 8, 00, 00);
    calendar.set(2014,5,1,19,55,00);
long when = calendar.getTimeInMillis();         // notification time


Log.d("time", when+" ");

Intent intentAlarm = new Intent(this, AlarmReceiver.class);

// create the object
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

 //set the alarm for particular time
alarmManager.set(AlarmManager.RTC_WAKEUP,when, PendingIntent.getBroadcast(this,1,  intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));

这是我的AlarmReceiver.java

Here is my AlarmReceiver.java

package com.myapp.app;

import java.util.Calendar;
import java.util.GregorianCalendar;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Calendar now = GregorianCalendar.getInstance();
        int dayOfWeek = now.get(Calendar.DATE);
        if(dayOfWeek != 5 && dayOfWeek != 7) {
            Notification.Builder mBuilder =
                    new Notification.Builder(context)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle("Here is my Title")
                    .setContentText("Here is my text");
            Intent resultIntent = new Intent(context, HomeActivity.class);
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
            stackBuilder.addParentStack(HomeActivity.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(1, mBuilder.build());
        }
    }
}

Manifest.xml

Manifest.xml

我在清单中添加了以下内容

I’ve added following in manifest

<receiver
android:name="com.myapp.app.AlarmReceiver"
/>

在此先感谢您的帮助...:)

Thanks in advance for your help... :)

推荐答案

您需要使用 setRepeating 每周重复一次警报。

You need to use the setRepeating to repeat your alarm every week.

alarmManager.setRepeating(AlarmManager.RTC, when, AlarmManager.INTERVAL_DAY * 7, pendingIntent);

P.S。不要设置年份。否则,您的闹钟只会触发一次。

P.S. Don't set the year. Otherwise, your alarm will trigger only once.

这篇关于我需要在我的android应用中实现Notification Reminder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 01:52