问题:
手机/模拟器会锁定重复的通知更新。让模拟器在锁定后恢复响应的唯一方法是按给定顺序按 Home => Menu => Lock => Home => Menu Button。
代码:
通知推送代码:
// Set up notifcation views
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); // Get notification manager
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.service_notification);
contentView.setViewVisibility(R.id.current_error_text, View.GONE);
contentView.setViewVisibility(R.id.error_text, View.GONE);
contentView.setViewVisibility(R.id.info_error_text, View.GONE);
contentView.setViewVisibility(R.id.info_text, View.GONE);
contentView.setViewVisibility(R.id.next_check_in_text, View.VISIBLE);
contentView.setViewVisibility(R.id.current_profile_text, View.VISIBLE);
contentView.setViewVisibility(R.id.profile_name_text, View.VISIBLE);
contentView.setTextViewText(R.id.next_check_in_text, mainText);
// Set profile text now
contentView.setTextViewText(R.id.profile_name_text, miniText);
// Set up a new notification
Notification notif = new Notification(R.drawable.service_logo_small, "Service is running", System.currentTimeMillis());
notif.contentView = contentView; // Set content view
// Create and plug in the PendingIntent
Intent notifIntent = new Intent(this, EntryPointActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, notifIntent, 0); // Set up the Pending Intent
notif.contentIntent = pIntent;
// Now set up notification flags
notif.flags |= Notification.FLAG_NO_CLEAR;
notif.flags |= Notification.FLAG_ONGOING_EVENT;
notif.flags |= Notification.FLAG_FOREGROUND_SERVICE;
if(sp.getBoolean("UpdateLights", true)) notif.flags |= Notification.DEFAULT_LIGHTS;
if(sp.getBoolean("UpdateVibrate", true)) notif.flags |= Notification.DEFAULT_VIBRATE;
if(sp.getBoolean("UpdateSound", true)) notif.flags |= Notification.DEFAULT_SOUND;
notificationManager.notify(R.string.app_name, notif);
所有对象都存在并且项目编译完美。我没有遇到 NullPointerExceptions!
调用通知创建函数的代码:
final Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask(){
@Override
public void run() {
if(( nextUpdateIn - System.currentTimeMillis() ) > 0) {
long milliseconds = (nextUpdateIn - System.currentTimeMillis());
int seconds = (int) (milliseconds / 1000) % 60 ;
int minutes = (int) ((milliseconds / (1000*60)) % 60);
String toShow = "Next Check In: " + minutes + " minute" + ((minutes != 1) ? "s" : "") + " " + seconds + " second" + ((seconds != 1) ? "s" : "");
pushNotification(STATE_LOADEDSUCCESSFULLY, currentProfile.getProfileName(), toShow);
} else {
currentState = STATE_RELOADING;
pushNotification(STATE_RELOADING, null, "Refreshing..");
timer.cancel();
}
}
}, 1, 999);
同样,所有对象都存在!通知在上述过程中已更新,但如上所述锁定了模拟器和电话!
目标:
更新状态栏中的通知以基本上显示倒计时,直到下一次刷新。
请帮助我尽快摆脱这个错误!
提前致谢 :)
问候,
阿克希特·苏塔
编辑:
我正在尝试通过SERVICE运行此代码,并且我已经尝试过运行 Android 2.2、2.3.3、4.1 的模拟器,它们都给了我同样的问题!