从外部类调用活动方法

时间:2011-12-28 09:53:56

标签: android android-activity android-context webchromeclient

我真的很困惑所有这些应用程序/活动上下文的东西。我想从类中调用一些活动方法,例如我自己的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); // ??
      }

}

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

1 个答案:

答案 0 :(得分:1)

我不太确定。

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;
}