如何在后台更新我的永久通知



我的通知显示电池电量。当我关闭应用程序时,通知中的百分比不会更新其状态。我不知道我该怎么做,现在我发布通知代码,我希望有人能帮助我

mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
            MainActivity.this);
    notificationBuilder.setOngoing(true);
    notificationBuilder.setContentTitle("Battery Stats Informations");
    notificationBuilder.setContentText("Battery level: " + level + "%"
            + " " + "Temp.: " + temperature + "°C");
    notificationBuilder.setTicker("Informazioni batteria");
    notificationBuilder.setWhen(System.currentTimeMillis());
    notificationBuilder.setSmallIcon(R.drawable.icon_small_not);
    Intent notificationIntent = new Intent(MainActivity.this,
            MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(
            MainActivity.this, 0, notificationIntent, 0);
    notificationBuilder.setContentIntent(contentIntent);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP
            | Intent.FLAG_ACTIVITY_NEW_TASK);
    Notification notification = new Notification();
    notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
    mNotificationManager.notify(1, notificationBuilder.build());
    mNotificationManager.notify(SIMPLE_NOTIFICATION_ID,
            notificationBuilder.build());

我什么都试过了,但都没成功。我该怎么办?感谢

如果你想在应用程序不工作时更新通知,你应该创建一个服务。您可以查看android开发人员网站

相关内容

  • 没有找到相关文章

最新更新