更新android中的日期状态栏图标



我开发了一个应用程序,它将状态栏中的日期显示为图标(每天1、2、3、…的图像),现在我的问题是,当日期发生变化时,我感觉不到它。有没有任何方法可以捕捉日期的变化并更新状态栏图标。

    mNM  =(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    int resId = getResources().getIdentifier("p_"+Day_Num, "drawable", getPackageName());
    Notification notification = new Notification(resId, "", System.currentTimeMillis());
    RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
    contentView.setImageViewResource(R.id.image, resId);
    contentView.setTextViewText(R.id.title, Day);
    contentView.setTextViewText(R.id.text, Date);
    contentView.setOnClickPendingIntent(R.id.imageView1, PendingIntent.getActivity(this, 0, new Intent(this, Option.class),0));
    notification.contentView = contentView;
    Intent notificationIntent = new Intent(this, Option.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.contentIntent = contentIntent;
    notification.defaults = Notification.FLAG_NO_CLEAR;
    mNM.notify(NOTIFICATION, notification);

当我使用servic Calservice.java时:

 public void onCreate() {
      //code to execute when the service is first created
       alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        Intent intent = new Intent(ALARM_REFRESH_ACTION);
        pendingIntent = PendingIntent.getBroadcast(this, 0, intent,
                PendingIntent.FLAG_CANCEL_CURRENT);
        alarmReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                alarmCounted++;
                Message msg = myHandler.obtainMessage(ALARM_CODE, intent);
                myHandler.sendMessage(msg);
            }
        };
        IntentFilter filter = new IntentFilter(ALARM_REFRESH_ACTION);
        registerReceiver(alarmReceiver, filter);
        mNM  =(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        utils.getCurrentShamsidate();
        startRepeating();
   }
       public void startRepeating() {
        // We get value for repeating alarm
        int startTime =1000;
        long intervals =1000;
        // We have to register to AlarmManager
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.MILLISECOND, startTime);
        // We set a repeating alarm
        alarmManager.setRepeating(AlarmManager.RTC, calendar
                .getTimeInMillis(), intervals, pendingIntent);
    }
            private void showNotification() {
    mNM.cancelAll();
    utils.getCurrentShamsidate();
    int resId = getResources().getIdentifier("p_"+Day_Num, "drawable", getPackageName());
    Notification notification = new Notification(resId, "", System.currentTimeMillis());
    RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
    contentView.setImageViewResource(R.id.image, resId);
    contentView.setTextViewText(R.id.title, Day);
    contentView.setTextViewText(R.id.text, Date);
    contentView.setOnClickPendingIntent(R.id.imageView1, PendingIntent.getActivity(this, 0, new Intent(this, Option.class),0));
    notification.contentView = contentView;
    Intent notificationIntent = new Intent(this, Option.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.contentIntent = contentIntent;
    mNM.notify(NOTIFICATION, notification);
  }
    public void doscan() {
            String Day = Day_Num;
            utils.getCurrentShamsidate();
            if(!Day_Num.equals(Day))
                showNotification();
        }

@hamhame,你为什么不试试AlarmManager,在每天早上12:00设置闹钟。这样你就可以很容易地设置你的操作和做事了。。

此链接可以帮助您了解如何设置24小时时间间隔的重复警报。

要收听TIme/用户更改日期,请参阅此答案。

您需要通过manifest注册一个接收器(上面的链接中给出了所需的操作)(如果想要始终侦听),或者根据您的需要在Activity/service中注册。在接收器中,您可以通过获取当前时间来了解设置的时间,也可以修改AlarmReceiver。

最新更新