PROBLEM:
The phone/emulator locks up on repeated Notification updates. The only way to get the emulator back responsive after it gets locked is by pressing Home => Menu => Lock => Home => Menu Button in that given order.
CODE:
Notification pushing code:
// 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);
All objects exist and the project compiles perfectly. I encounter NO NullPointerExceptions!
Code calling the notification creating function:
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);
Again, all objects exist! The notification IS UPDATED in the above process but it locks up BOTH THE EMULATOR AND PHONE as mentioned above!
AIM:
To update notification in the Status Bar to basically show a countdown till the next refresh.
Please help me get rid of this error ASAP!
Thanks in advance :)
Regards,
Akshit Soota
EDIT:
I'm trying to run this code via a SERVICE and I've tried on emulators running Android 2.2, 2.3.3, 4.1 and all of them give me the same problem!
Your code for displaying and updating notifications works just fine.
It seems to me that the only cause for this could be that many timers are being constructed simultaneously. Since the initial delay is 1 millisecond, you may be locking up the system with hundreds of notification updates if timer.scheduleAtFixedRate()
is being called through some unintended loop. I would suggest placing a breakpoint on that line and figuring out why that's happening.
I have two additional suggestions:
Notification
constructor is now deprecated, and you should probably consider using a notification builder such as Jake Wharton's NotificationCompat2.ScheduledThreadPoolExecutor
instead of Timer
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With