Android每日通知无法正常运行

时间:2017-01-12 22:18:49

标签: android

我希望每天自动通知我的时间。

我参与了创作的主要活动:

Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 16);
    calendar.set(Calendar.MINUTE, 35);
    calendar.set(Calendar.SECOND, 0);
    Intent intent1 = new Intent(ActivitySplashScreen.this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(ActivitySplashScreen.this, 0,intent1, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) ActivitySplashScreen.this.getSystemService(ActivitySplashScreen.this.ALARM_SERVICE);
    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

我的AlarmReceiver:

public class AlarmReceiver extends BroadcastReceiver{

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

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

        Intent notificationIntent = new Intent(context, ActivitySplashScreen.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.ic_invite_friends)
                .setContentTitle("Yeah!")
                .setContentText("Do not forget to earn zeni!")
                .setSound(alarmSound)
                .setAutoCancel(true).setWhen(when)
                .setContentIntent(pendingIntent)
                .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
        notificationManager.notify((int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE), mNotifyBuilder.build());


    }

}

问题是,每次应用创建时都会显示,并且仅在用户打开它的那一天显示。当用户在第二天没有打开应用程序时,它没有显示。

1 个答案:

答案 0 :(得分:0)

  Calendar cal = Calendar.getInstance();
            cal.set(Calendar.HOUR_OF_DAY, 1);
            cal.set(Calendar.MINUTE, 00);



            Calendar cur_cal = new GregorianCalendar();
            cur_cal.setTimeInMillis(System.currentTimeMillis());//set the current time and date for this calendar


            Calendar cal = new GregorianCalendar();
            cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
            cal.set(Calendar.HOUR_OF_DAY, 00);
            cal.set(Calendar.MINUTE, 02);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE) + 1);
            cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));


            //Create a new PendingIntent and add it to the AlarmManager
            Intent intent = new Intent(getContext(), YourCustomBroadcast.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext(),
                    12345, intent, PendingIntent.FLAG_CANCEL_CURRENT);

            AlarmManager am =
                    (AlarmManager) getContext().getSystemService(Activity.ALARM_SERVICE);
            am.cancel(pendingIntent);
            am.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

尝试使用此代码,在Calendar实例中提供每日通知的确切时间 创建自己的自定义Broadcast类,如下所示

public class YourCustomBroadcast extends BroadcastReceiver {
private final String SOMEACTION = "packagename.ACTION";
public static int notId = 1;

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

    Log.d("inside broadcast", "onReceive: ");

    //Toast.makeText(context, "call again", Toast.LENGTH_SHORT).show();

    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(System.currentTimeMillis());


    NotificationCompat.Builder mBuilder =
            (NotificationCompat.Builder) new NotificationCompat.Builder(context)
                    .setSmallIcon(android.R.drawable.sym_def_app_icon)
                    .setContentTitle("Day changed")
                    .setContentText("" + c.getTime().toString());

    NotificationManager mNotificationManager =

            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);


    mNotificationManager.notify(++notId, mBuilder.build());



}

}