0

我开发了一个应用程序,将状态栏中的日期显示为图标(每天的图像,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);

当我使用服务 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();
        }
4

1 回答 1

1

@hamhame,你为什么不试试AlarmManager,在每天早上12:00 设置闹钟。通过这种方式,您可以设置您的操作并很容易地做事..

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

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

您需要通过清单(如果想一直收听)注册一个接收器(上面链接中给出了所需的操作)或根据您的需要在活动/服务中注册。在接收器中,您将通过获取当前时间了解已设置的时间,并且也可以修改您的 AlarmReceiver。

于 2012-07-12T18:23:21.587 回答