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