打开“通知”时,通知栏滞后

时间:2013-07-12 10:11:28

标签: android broadcastreceiver android-preferences android-notifications batterylevel

我的应用程序在通知栏中显示一条通知,显示电池电量。通知有效,但是当点击首选项中的复选框并显示通知时,我会尝试下拉通知栏并且它会滞后。我不知道方式,但我认为这是因为通知检查每秒电池电量,但我不确定..我发布代码:

@SuppressLint("NewApi")
    private void checkPref(Intent intent){ 
            this.registerReceiver(this.batteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
            SharedPreferences myPref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this); 
            boolean pref_opt1 = myPref.getBoolean("firstDependent", false); 
            int level= intent.getIntExtra(BatteryManager.EXTRA_LEVEL,-1);
            if (pref_opt1){
                NotificationManager notifi = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
                 Notification notification = new Notification.Builder(getApplicationContext())
                 .setContentTitle("Battery Informations")
                 .setContentText("Battery level"+" "+level+"%")
                 .setSmallIcon(R.drawable.icon_small_not)
                 //.setLargeIcon(aBitmap)
                 .setTicker(level+"%")
                 .build();

                 notification.flags = Notification.FLAG_ONGOING_EVENT;
                 Intent i = new Intent(MainActivity.this, MainActivity.class); 
                 PendingIntent penInt = PendingIntent.getActivity(getApplicationContext(), 0 , i , 0);
                 notifi.notify(215,notification);
                } else {
                NotificationManager notifi = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
                notifi.cancel(215);
                }
        }

问题可能就是这个this.registerReceiver(this.batteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));。但我知道只有这样才能在电池电量变化时更新通知..有些有帮助吗?

1 个答案:

答案 0 :(得分:0)

检查显示通知的“if-case”代码是否重复执行。如果是这样,请尝试通过标记值限制显示通知,并在notifi.cancel()期间重置标记。