每次打开应用程序时,计划通知都会触发

时间:2015-12-04 11:44:20

标签: android notifications broadcastreceiver

我正在构建一个应用程序,它会在11:30 IST触发通知。我已经测试了它的工作但是在11:30之后每当我打开我的应用程序时它会发出通知......

我不知道为什么我认为在时间检查条件下应该是一些错误......

我希望通知只能在一天中按时触发一次......然后是第二天...... :)

请解决此问题 提前谢谢

**MainActivity.java**

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (ProgressBar)findViewById(R.id.progressBar);
spinner.setVisibility(View.GONE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 11);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 00);
Intent intent1 = new Intent(MainActivity.this, AlarmReciever.class);
PendingIntent pendingIntent   = PendingIntent.getBroadcast(MainActivity.this,0,intent1, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE);
    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}


**AlarmReciever.java**

package com.alivestats.alivestats;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
public class AlarmReciever extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    int MID = 1;

    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(context, MainActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);


    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
            context).setSmallIcon(R.drawable.background)
            .setContentTitle("title")
            .setContentText("description").setSound(alarmSound)
            .setAutoCancel(true).setWhen(when)
            .setContentIntent(pendingIntent)
            .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
    notificationManager.notify(MID, mNotifyBuilder.build());
    MID++;

}

}

5 个答案:

答案 0 :(得分:3)

经过大量的茶和咖啡,我自己编写了答案...

long t = System.currentTimeMillis();
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 11);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 00);
if (t <= calendar.getTimeInMillis()) {
        //calendar.add(Calendar.DATE, 6); //add 6 days.
        Intent intent1 = new Intent(MainActivity.this, AlarmReciever.class);
        intent1.setAction("NOTE");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        //am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
    }

    else{

    }

我刚刚将系统时间与通知时间进行了比较..如果通知时间小于等于系统时间,则其真实和接收器将起作用..但如果没有,那么什么都不会发生......

BTW感谢您帮助@tiny和@Mahesh

答案 1 :(得分:0)

Intent intent1 = new Intent(MainActivity.this, AlarmReciever.class);
intent1 .setAction("ilove");


public void onReceive(Context context, Intent intent) {
 if(!"ilove" .equalsIgnoreCase(getAction()))log.e("test",intent.getAction());
}


<receiver android:name="com.android.app.AlaramReceiver" android:exported="false">
 <intent-filter>
<action android:name="ilove" /> 
</intent-filter>
</receiver>

答案 2 :(得分:0)

日历日历= Calendar.getInstance();

          calendar.set(Calendar.HOUR, 6);

    int myPreferedTime=calendar.get(Calendar.HOUR);


    Calendar rightNow = Calendar.getInstance();

    int currentHour = rightNow.get(Calendar.HOUR_OF_DAY);
    Log.i("current time","is"+currentHour);


    int difference=myPreferedTime-currentHour;

    Log.i("Differencevalue","is"+difference);

    switch(difference)
    {
        case 1:

            rightNow.add(Calendar.HOUR_OF_DAY, 1);
            break;

        case 2:

            rightNow.add(Calendar.HOUR_OF_DAY, 2);
            break;

        case 3:

            rightNow.add(Calendar.HOUR_OF_DAY, 3);
            break;

        case 4:

            rightNow.add(Calendar.HOUR_OF_DAY, 4);
            break;

        case 5:

            rightNow.add(Calendar.HOUR_OF_DAY, 5);
            break;
        case 6:

            rightNow.add(Calendar.HOUR_OF_DAY, 0);
            break;

        case -1:
            rightNow.add(Calendar.HOUR_OF_DAY,23);
            break;

        case -2:
            rightNow.add(Calendar.HOUR_OF_DAY,22);
            break;

        case -3:
            rightNow.add(Calendar.HOUR_OF_DAY,21);
            break;
        case -4:
            rightNow.add(Calendar.HOUR_OF_DAY,20);
            break;
        case -5:
            rightNow.add(Calendar.HOUR_OF_DAY,19);
            break;

        case -6:
            rightNow.add(Calendar.HOUR_OF_DAY,18);
            break;
        case -7:
            rightNow.add(Calendar.HOUR_OF_DAY,17);
            break;
        case -8:
            rightNow.add(Calendar.HOUR_OF_DAY,16);
            break;
        case -9:
            rightNow.add(Calendar.HOUR_OF_DAY,15);
            break;
        case -10:
            rightNow.add(Calendar.HOUR_OF_DAY,14);
            break;
        case -11:
            rightNow.add(Calendar.HOUR_OF_DAY,13);
            break;
        case -12:
            rightNow.add(Calendar.HOUR_OF_DAY,12);
            break;
        case -13:
            rightNow.add(Calendar.HOUR_OF_DAY,11);
            break;
        case -14:
            rightNow.add(Calendar.HOUR_OF_DAY,10);
            break;
        case -15:
            rightNow.add(Calendar.HOUR_OF_DAY,9);
            break;

        case -16:
            Log.i("am here","bcoz time is 11 clock");
            rightNow.add(Calendar.HOUR_OF_DAY, 8);
            break;

        case -17:
            rightNow.add(Calendar.HOUR_OF_DAY,7);
            break;
        case -18:
            rightNow.add(Calendar.HOUR_OF_DAY,6);
            break;
        default:
            break;


    }

        Intent intent1 = new Intent(MainActivity.this, MyReceiver.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent1, 0);
        Log.i("am sending", "yes");
        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        Log.i("time holding with me", "calendar stores time like" + rightNow.getTimeInMillis());
        am.setRepeating(AlarmManager.RTC_WAKEUP, rightNow.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, pendingIntent);

答案 3 :(得分:0)

为了防止警报在每次启动应用程序时持续触发(在警报时间过后),可以添加以下内容:

    Calendar timeNow = Calendar.getInstance();
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 11);
    calendar.set(Calendar.MINUTE, 30);
    if(calendar.before(timeNow)){
        calendar.add(Calendar.DATE, 1);
    }
    mAlarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, mAlarmPendingIntent);

我找到了上面的例子here。现在将根据闹钟时间检查时间,如果闹钟时间在现在时间之前,则会在闹钟中添加另一天。 希望这有助于:)。

答案 4 :(得分:0)

我也有这个问题&amp;尝试了这个页面上的每一个建议,我无法解决我的问题。以下是针对此问题的建议解决方案,它适用于我。

在manifest.xml中注册接收器

<receiver android:name=".receivers.DayAlarmReceiver"/>

设置闹钟管理器

public void setDayAlarm(Context context) {

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 4);

AlarmManager alarmMgr = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent alarmIntent = new Intent(context, DayAlarmReceiver.class);
alarmIntent.putExtra("HOUR_OF_DAY", 4);

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 5555, alarmIntent, 0);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pendingIntent);
Log.d("Set the day alarm");
}

检查接收者类的onReceive()方法中的意图附加内容

@Override
public void onReceive(Context context, Intent intent) {
  int setHour = intent.getIntExtra("HOUR_OF_DAY", -1);
  Calendar rightNow = Calendar.getInstance();
  int currentHour = rightNow.get(Calendar.HOUR_OF_DAY);

  if(currentHour == setHour){
    Log.d("This is perfect, Trigger the alarm");
    showNotification(context, msg);
  }else{
   Log.d("This is perfect, Trigger the alarm");
 }