Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我需要在我的android应用程序中实现通知提醒_Android_Notifications_Broadcastreceiver_Alarmmanager_Reminders - Fatal编程技术网

我需要在我的android应用程序中实现通知提醒

我需要在我的android应用程序中实现通知提醒,android,notifications,broadcastreceiver,alarmmanager,reminders,Android,Notifications,Broadcastreceiver,Alarmmanager,Reminders,我的要求:我希望我的应用程序在每周五上午8点显示提醒通知 我使用AlarmManager、BroadcastReceivers来实现提醒通知。我遇到的问题是,当我使用当前系统时间设置通知时间并在其中添加2分钟时。。。当我以这种方式使用它时,它会在2分钟后触发我的通知 但是, 当我使用calender实例在任何一天的特定时间设置通知时间时,它会在我在设备/模拟器上启动/打开应用程序时触发提醒通知,其次,它不会在指定时间触发通知 以下是我的课程 这是我的HomeActivity.java // lo

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

我使用AlarmManager、BroadcastReceivers来实现提醒通知。我遇到的问题是,当我使用当前系统时间设置通知时间并在其中添加2分钟时。。。当我以这种方式使用它时,它会在2分钟后触发我的通知

但是,

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

以下是我的课程

这是我的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

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

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

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


提前感谢您的帮助……:)

您需要使用
设置repeating
每周重复您的报警

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

注:不要设定年份。否则,您的闹钟只会触发一次。

您的
日历设置已过时。注释掉的
calendar.set
使用一个月中的一天作为星期天,该天显示为1,因此它总是在相应月份的第一天(2014年后将关闭)。