gpt4 book ai didi

android - 与服务分离的 WebView ?

转载 作者:行者123 更新时间:2023-12-05 07:55:54 24 4
gpt4 key购买 nike

当我的应用程序退出时(后退按钮、exitApp 调用、任务 killer 等),它会留下一个分离的 webview,我可以在 Chrome 的检查器中看到它;即使我的应用程序已关闭, WebView 仍在运行。但是,如果我不启动前台服务,webview 将按预期关闭。

我想解决这个问题——既要善待用户的内存,又要防止僵尸 WebView 工作并造成破坏。

在我看来,webview 和服务之间有一些引用(webview 有一些服务引用,反之亦然),但我找不到连接 - 我启动了服务,我没有引用到它,它通过广播 Intent 与我的 CordovaPlugin 类通信。

这是一个普遍的问题吗,如果一个应用程序运行一个服务那么它的 webview 不会死?如果不是,我怎样才能找到使 webview 保持 Activity 状态的原因?

编辑 - 一条评论要求代码,所以我创建了一个示例项目...... startForeground 调用导致僵尸 WebView (没有这个调用, WebView 关闭而不是保持打开状态为“分离”)。

package com.example.plugin;

import org.apache.cordova.*;
import org.json.JSONArray;
import org.json.JSONException;
import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.app.PendingIntent;
import android.util.Log;
import android.app.Service;
import android.os.IBinder;

public class Hello extends CordovaPlugin {

private final static String LOG_TAG = "****** SERVICE STARTER *******";

@Override
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {
if (action.equals("startService")) {
startService();
return true;
} else {
return false;
}
}

private void startService() {
Context context = this.cordova.getActivity().getApplicationContext();
Intent service = new Intent(context, EmptyService.class);
context.startService(service);
}

public final static class EmptyService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
// zombies created if this startForeground call is included
startForeground(1, getNotification());
return Service.START_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
return null;
}

private Notification getNotification() {
Notification.Builder builder = new Notification.Builder(this);
builder
.setContentTitle("Service")
.setContentText("Running in fg, baby");
Notification notification = builder.build();
notification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE | Notification.FLAG_NO_CLEAR;
return notification;
}
}

}

最佳答案

这显然是 Cordova 中的一个错误 - Cordova 5.0.0 没有此行为,因此从 3.6 升级到 5.0 为我修复了它。

关于android - 与服务分离的 WebView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29284018/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com