Как отобразить процент заряда батареи при уведомлении и обновлении с помощью службы переднего плана в android? - PullRequest
0 голосов
/ 21 июня 2020

Я хочу, чтобы служба обновляла процент заряда батареи в уведомлении с помощью службы переднего плана, приведенный ниже код - это моя служба уведомлений, как обновить процент заряда батареи и выполнить другой процесс в классе обслуживания для обновления уведомления?

publi c class ShowNotificationIntentService расширяет IntentService {private stati c final String ACTION_SHOW_NOTIFICATION = "my.app.service.action.show"; частное состояние c конечная строка ACTION_HIDE_NOTIFICATION = "my.app.service.action.hide";

RemoteViews contentView;

public ShowNotificationIntentService() {
    super("ShowNotificationIntentService");

    contentView = new RemoteViews(getPackageName(), R.layout.custom_push);
    contentView.setImageViewResource(R.id.image, R.mipmap.ic_launcher);
    contentView.setTextViewText(R.id.title, "1%");
    contentView.setTextViewText(R.id.text, ""+battery_level());
    contentView.setTextViewText(R.id.ischarging, ""+ischarging(this));


}

public static boolean ischarging(Context context) {
    boolean isPlugged= false;
    Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    isPlugged = plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB;
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
        isPlugged = isPlugged || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
    }
    return isPlugged;
}

private int battery_level() {
    int batLevel=0;
    BatteryManager bm = (BatteryManager) getSystemService(BATTERY_SERVICE);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        batLevel = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
        int status = bm.getIntProperty(BatteryManager.BATTERY_PLUGGED_AC);

        return batLevel;
    }
    else
    {
        batLevel = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
        return batLevel;
    }
}

public static void startActionShow(Context context) {
    Intent intent = new Intent(context, ShowNotificationIntentService.class);
    intent.setAction(ACTION_SHOW_NOTIFICATION);
    context.startService(intent);
}

public static void startActionHide(Context context) {
    Intent intent = new Intent(context, ShowNotificationIntentService.class);
    intent.setAction(ACTION_HIDE_NOTIFICATION);
    context.startService(intent);
}

@Override
protected void onHandleIntent(Intent intent) {
    if (intent != null) {
        final String action = intent.getAction();
        if (ACTION_SHOW_NOTIFICATION.equals(action)) {
            handleActionShow();
        }
    }
}

private void handleActionShow() {
    showStatusBarIcon(ShowNotificationIntentService.this,contentView);
}

public static void showStatusBarIcon(Context ctx,  RemoteViews cv) {
    Context context = ctx;

    NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx)
            .setContent(cv)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setSound(null,0)
            .setOngoing(true);
    Intent intent = new Intent(context, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
    builder.setContentIntent(pendingIntent);
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notif = builder.build();
    notif.flags |= Notification.FLAG_ONGOING_EVENT;
    mNotificationManager.notify(1, notif);
}

}

...