5

问题:

手机/模拟器会锁定重复的通知更新。让模拟器在锁定后恢复响应的唯一方法是按给定顺序按 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 的模拟器,它们都给了我同样的问题!

4

3 回答 3

2

您用于显示和更新通知的代码工作正常。

在我看来,唯一的原因可能是同时构建了许多计时器。timer.scheduleAtFixedRate()由于初始延迟为 1 毫秒,如果通过某个意外循环调用,您可能会使用数百个通知更新锁定系统。我建议在该行上放置一个断点并找出发生这种情况的原因。

我有两个额外的建议:

  1. 请记住,Notification现在不推荐使用构造函数,您可能应该考虑使用通知构建器,例如 Jake Wharton 的NotificationCompat2
  2. 建议使用ScheduledThreadPoolExecutor而不是Timer.
于 2012-11-14T10:14:05.450 回答
2

我同意@Paul Lammertsma 的观点,假设您的代码会产生很大的负载。但是,每 999 毫秒执行一次计时器任务不会有问题,我想问题出在您的“通知推送代码”中。无论如何,您应该使用方法分析来确定瓶颈是什么。

PS我认为,您的代码中有一个小错误:

if (( nextUpdateIn - System.currentTimeMillis() ) > 0) {
    long milliseconds = (nextUpdateIn - System.currentTimeMillis());
    // milliseconds may still be negative here
    ...

你可能想要这个:

long milliseconds = nextUpdateIn - System.currentTimeMillis();
if (milliseconds > 0) {
    ...

PPS 为什么要用Notification.FLAG_FOREGROUND_SERVICEflag?

于 2012-11-15T14:56:50.767 回答
1

好吧,我终于知道问题出在哪里了。

您可能也对此链接感兴趣:http ://code.google.com/p/android/issues/detail?id=13941#c12

因此,根据上面的链接,每次调用通知推送函数时,我都会停止创建一个新的Notification对象。现在,我创建了一次对象并继续重复使用它!这已经停止了模拟器锁定!

以下是通知推送代码的修改:

    // Set up notifcation views
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    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
    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);

您可能会看到NotificationManagerNotification不再在通知推送代码中创建。

以下是我对计时器所做的更改:

NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); // Get notification manager
Notification notif = new Notification(R.drawable.service_logo_small, "Service is running", System.currentTimeMillis());
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);

如您所见,我在计时器中重用了对象notificationManagernotif,因此 CPU 不会在每次执行 Timer 时重新创建 Notification 对象。

因此,通过上述代码更改,我的代码现在可以完美运行!

谢谢你帮助我。

于 2012-11-18T13:39:15.987 回答