Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.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 - Fatal编程技术网

Android每日通知无法正常工作

Android每日通知无法正常工作,android,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,

我想每天自动通知我的时间

我在创建时的主要活动中:

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);
还有我的闹钟接收器:

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());


    }

}
问题是,这显示了应用程序创建的每一次时间,只有在用户打开它的那一天。当用户在第二天未打开应用程序时,它不会显示

  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);
尝试使用此代码,在日历实例中提供每日通知的准确时间 创建自己的自定义广播类,如下所示

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());



}
}

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

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());



}

}

代码看起来不错。我尝试了一个较短的时间间隔(两个报警实例之间的间隔为5分钟),即使应用程序未运行且设备处于睡眠模式,它似乎也能工作。这来自Android文档-注意:从API 19开始,所有重复的报警都是不精确的。如果您的应用程序需要精确的交付时间,那么它必须使用一次性精确警报,并按上述方式重新安排每次的时间。targetSdkVersion早于API 19的传统应用程序将继续将其所有报警(包括重复报警)视为精确报警。简而言之,这完全取决于OS。持续时间较长的另一件事是,你必须考虑用户可能在那个时间间隔重新启动电话的可能性。并非所有警报都能在设备重启后继续存在(请阅读此页面上的Android行为)。请参阅堆栈上的此问答-。在这种情况下,一旦用户在重新启动后启动应用程序,警报就会被安排,如果已经超过了一天中的时间(如警报创建中所述),警报会立即触发代码看起来很正常。我尝试了一个较短的时间间隔(两个报警实例之间的间隔为5分钟),即使应用程序未运行且设备处于睡眠模式,它似乎也能工作。这来自Android文档-注意:从API 19开始,所有重复的报警都是不精确的。如果您的应用程序需要精确的交付时间,那么它必须使用一次性精确警报,并按上述方式重新安排每次的时间。targetSdkVersion早于API 19的传统应用程序将继续将其所有报警(包括重复报警)视为精确报警。简而言之,这完全取决于OS。持续时间较长的另一件事是,你必须考虑用户可能在那个时间间隔重新启动电话的可能性。并非所有警报都能在设备重启后继续存在(请阅读此页面上的Android行为)。请参阅堆栈上的此问答-。在这种情况下,只要用户在重新启动后启动应用程序,警报就会被安排好,如果已经超过了一天中的时间(如警报创建中所述),警报就会立即触发