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

Android使用计时器自动更新通知

Android使用计时器自动更新通知,android,timer,notifications,Android,Timer,Notifications,我希望我的活动每5分钟刷新一次,然后根据刷新的值显示通知。我已经有了一个刷新按钮,它工作得很好。但当我试图在计时器中出现通知之前激活刷新按钮时。我的应用程序崩溃了 这是我的密码 //一次创建 if(ordercounter>0) //ordercounter is the updated value { MyTimerTask myTask = new MyTimerTask();

我希望我的活动每5分钟刷新一次,然后根据刷新的值显示通知。我已经有了一个刷新按钮,它工作得很好。但当我试图在计时器中出现通知之前激活刷新按钮时。我的应用程序崩溃了

这是我的密码

//一次创建

            if(ordercounter>0) //ordercounter is the updated value
            {

             MyTimerTask myTask = new MyTimerTask();

                Timer myTimer = new Timer();
                myTimer.schedule(myTask, 1, 300000);
            }
//计时器

class MyTimerTask extends TimerTask {
    public void run() {

        refresh.performClick(); // this line is the cause of error pls help
        generateNotification(getApplicationContext(), "You have "+ ordercounter+ " pending orders!");
    }
}

private void generateNotification(Context context, String message) {

    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    String appname = context.getResources().getString(R.string.app_name);
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notification;
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            new Intent(context, Admin.class), 0);


        NotificationCompat.Builder builder = new NotificationCompat.Builder(
                context);
        notification = builder.setContentIntent(contentIntent)
                .setSmallIcon(icon).setTicker(appname).setWhen(0)
                .setAutoCancel(true).setContentTitle(appname)
                .setContentText(message).build();

        notificationManager.notify((int) when, notification);




}
//我的刷新按钮

refresh.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
             Intent intent = getIntent();
             intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                finish();
                startActivity(intent);

        }
    });

我认为您应该使用AlarmManager,并将其设置为每5分钟激活一次:

Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            millisecondsToFirstActivation,
            millisecondsForRepeatingSchedule, alarmIntent);
您创建的广播接收器将显示通知

public class MyAlarmReceiver extends BroadcastReceiver {

    @Override 
    public void onReceive(Context context, Intent intent) {
        displayNotification();
    } 
} 
您可以在此处找到更多信息:

您能显示错误日志吗?另外,即使应用程序进入睡眠状态,您也需要进行此刷新吗?如果是这样,您可能应该使用AlarmManager而不是TimerTaskies。我希望它进行刷新,以便更新下一个通知,但我不希望它在应用程序休眠时重新打开。您好!我试图在我的oncreate上输入第一个代码,然后在oncreate后输入第二个代码,但由于某些原因,我的应用程序无法打开