Android 打开“正在进行的通知”时,通知栏会滞后
我的应用程序在通知栏中有一个通知,显示电池电量。通知可以工作,但是当首选项中的复选框被单击并且通知出现时,我试图拉下通知栏,但它会延迟。我不知道怎么办,但我认为这是因为通知每秒都会检查电池电量,但我不确定。。我发布代码:Android 打开“正在进行的通知”时,通知栏会滞后,android,broadcastreceiver,android-preferences,android-notifications,batterylevel,Android,Broadcastreceiver,Android Preferences,Android Notifications,Batterylevel,我的应用程序在通知栏中有一个通知,显示电池电量。通知可以工作,但是当首选项中的复选框被单击并且通知出现时,我试图拉下通知栏,但它会延迟。我不知道怎么办,但我认为这是因为通知每秒都会检查电池电量,但我不确定。。我发布代码: @SuppressLint("NewApi") private void checkPref(Intent intent){ this.registerReceiver(this.batteryInfoReceiver, new IntentF
@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.batteryForeceiver,新的IntentFilter(Intent.ACTION\u BATTERY\u CHANGED))代码>。但我只知道在电池电量变化时更新通知的方法。。请提供一些帮助?检查显示通知的“if case”代码是否重复执行。如果是这样,请尝试通过标志值将显示通知限制为一次,并在notifi.cancel()期间重置标志