从外部类调用活动方法

发布于 2024-12-23 01:53:13 字数 819 浏览 1 评论 0原文

我真的对所有这些应用程序/活动上下文的东西感到困惑。我想从类中调用一些活动方法,例如我自己的 WebChromeClientConnectionChangeReceiver (检测网络连接更改)。

示例代码:

public class MyWebChromeClient extends WebChromeClient {

    MyWebView webview;
    MyApp app;

// own constructor to store info about webview which is used in the current    
//webchrome client
    public MyWebChromeClient(Context context, MyWebView wv)
    {
        this.webview = wv; // store information about current webview (leaks?)
        this.app = context.getApplicationContext(); // store app context 
    }

    public void onConsoleMessage(String message, int lineNumber, String sourceID) {
           app.getActivityWhichIsUsingThisWebView().logFromWebView(webview, message); // ??
      }

}

如何实现?是否有任何合理的模式可以帮助我避免内存泄漏?

I am really confused with all this application/activity context stuff. I want to call some of activity methods from classes, such as my own WebChromeClient or ConnectionChangeReceiver (detecting network connection changes).

Example code:

public class MyWebChromeClient extends WebChromeClient {

    MyWebView webview;
    MyApp app;

// own constructor to store info about webview which is used in the current    
//webchrome client
    public MyWebChromeClient(Context context, MyWebView wv)
    {
        this.webview = wv; // store information about current webview (leaks?)
        this.app = context.getApplicationContext(); // store app context 
    }

    public void onConsoleMessage(String message, int lineNumber, String sourceID) {
           app.getActivityWhichIsUsingThisWebView().logFromWebView(webview, message); // ??
      }

}

How to achieve that? Is there any sensible pattern which could help me avoiding memory leaks?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺渁∝ 2024-12-30 01:53:13

我不太确定。

Activity activity;

public MyWebChromeClient(Context context, MyWebView wv,Activity activity)
{
    this.webview = wv; // store information about current webview (leaks?)
    this.app = context.getApplicationContext(); // store app context 
    this.activity = activity;
}

i'm not quite sure.

Activity activity;

public MyWebChromeClient(Context context, MyWebView wv,Activity activity)
{
    this.webview = wv; // store information about current webview (leaks?)
    this.app = context.getApplicationContext(); // store app context 
    this.activity = activity;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文