首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更新android中的日期状态栏图标

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

Stack Overflow用户
提问于 2012-07-13 00:55:07
回答 1查看 711关注 0票数 0

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

代码语言:javascript
复制
    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时:

代码语言:javascript
复制
 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();
        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-13 02:23:22

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

此link可帮助您更多地了解如何设置24小时间隔的重复闹钟。

用于监听用户 see this answer对时间/日期的更改。

您需要根据需要通过清单(如果想要始终监听)或在活动/服务中注册接收器(具有上述链接中给出的所需操作)。在接收器中,你可以通过获取当前时间来知道设置了什么时间,还可以修改你的AlarmReceiver。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11456786

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档