0

I develop an app that shows date in status bar as icon(an image for every day,1,2,3,...),now my problem is that when the date change i can't sense it. is there any way that can catch the change of date and update the status bar icon.

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

when I use 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
  • 47
  • 1
  • 10

1 Answers1

1

@hamhame, Why don't you try an AlarmManager, to set alarm on Every morning at 12:00 Am. in this way you can set your opration and do things pretty easily..

This link can help you more on how to set a repeating alarm for Time interval of 24 hours.

for listening to TIme/Date change by user see this answer.

you need to register a receiver(with required action given in above link) via manifest (if want to listen always ) or register in Activity/service according to your need. and in receiver you will got to know what time has been set by getting current time and can modify you AlarmReceiver too.

Community
  • 1
  • 1
AAnkit
  • 27,299
  • 12
  • 60
  • 71
  • by this way when user change time manually,I cant change the icon – Hamhame Jul 13 '12 at 04:20
  • Thank you for your answer but I have problem with static {...... where I should add this? – Hamhame Jul 14 '12 at 05:48
  • You better register those Intent Action in Manifest file. under Receiver tag – AAnkit Jul 14 '12 at 07:31
  • Please see edited question,I use service for updating notification when I use debug mode it works well and when I change the date notification changes but when I run it no change happens. – Hamhame Jul 14 '12 at 08:05
  • for this case, create a receiver register it with intent Action which that answers shows.. Static block is just used to execute some code once – AAnkit Jul 14 '12 at 08:13
  • Now,I have just one Problem that is when I change date to future it works but when I change it to past not,I think that the problem relates to alarm manager setting but I dont know how to solve it – Hamhame Jul 14 '12 at 08:49
  • if you set it to past, AlarmManager will trigger an alarm immediately.. you should set all your alarm event according to current time – AAnkit Jul 14 '12 at 08:51
  • could you please explain more?,I use this alarmManager.setRepeating(AlarmManager.RTC, calendar .getTimeInMillis(), intervals, pendingIntent); – Hamhame Jul 14 '12 at 09:02