NotificationManager.notify锁定手机和模拟器

Axe*_*Axe 5 android android-notifications

问题:

电话/模拟器会锁定重复的通知更新.在锁定之后让仿真器恢复响应的唯一方法是按给定的顺序按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);
Run Code Online (Sandbox Code Playgroud)

存在所有对象,项目编译完美.我遇到没有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);
Run Code Online (Sandbox Code Playgroud)

同样,所有对象都存在!通知在上述过程中更新了,但它如上所述锁定了仿真器和电话!

目标:

更新状态栏中的通知以基本显示倒计时直到下一次刷新.

请尽快帮助我摆脱这个错误!

提前致谢 :)

问候,

Akshit Soota

编辑:

我正在尝试通过SERVICE运行此代码,我尝试了运行Android 2.2,2.3.3,4.1的模拟器,所有这些都给了我同样的问题!

Axe*_*Axe 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);
Run Code Online (Sandbox Code Playgroud)

您可能会看到通知推送代码中不再创建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);
Run Code Online (Sandbox Code Playgroud)

正如您可能看到的,我在计时器中重用了对象notificationManagernotif,因此每次执行计时器时,CPU 都不会承担重新创建通知对象的负担。

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

谢谢你帮助我。