Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android JavascriptInterface使用上下文调用活动中的方法(OOP)_Android_Oop - Fatal编程技术网

Android JavascriptInterface使用上下文调用活动中的方法(OOP)

Android JavascriptInterface使用上下文调用活动中的方法(OOP),android,oop,Android,Oop,在OOP中,我可以使用对象调用其方法。所以我想在安卓系统中使用同样的概念。但是,它不起作用。我使用context调用函数updatevl(int),但它说无法解析该方法。我想知道如何使用上下文调用方法updatevl public class MainActivity extends Activity { private WebView webview; SharedPreferences sharedPref; SharedPreferences.Editor editor; @Overr

OOP
中,我可以使用对象调用其方法。所以我想在安卓系统中使用同样的概念。但是,它不起作用。我使用
context
调用函数
updatevl(int)
,但它说无法解析该方法。我想知道如何使用
上下文
调用方法
updatevl

public class MainActivity extends Activity {

private WebView webview;
SharedPreferences sharedPref;
SharedPreferences.Editor editor;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);


    sharedPref = this.getPreferences(0);
    editor = sharedPref.edit();

    webview = new WebView(this);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.addJavascriptInterface(new WebViewJavaScriptInterface2(this), "app");


public void updateLvl(int newLvl){
    sharedPref = this.getPreferences(0);
    editor = sharedPref.edit();
    int be4Level = sharedPref.getInt("currLevel", 1);
    if (newLvl >= be4Level){
        editor.putInt("currLevel", newLvl);
        editor.commit();
    }
  }


}

class WebViewJavaScriptInterface2{

private Context context;
/*
 * Need a reference to the context in order to sent a post message
 */
public WebViewJavaScriptInterface2(Context context){
    this.context = context;
}

@JavascriptInterface
public void openLvl(int lvl){
    context.updateLvl(lvl);
}

}

您需要将
context
强制转换为
MainActivity
,因为
MainActivity
类具有
updateVL
方法而不是
context
类,因此编译器将在应用静态绑定时向您显示错误

@JavascriptInterface
public void openLvl(int lvl){
    if(context instanceof MainActivity) // add safety check if required
        ((MainActivity)context).updateLvl(lvl);
}

您需要将
context
强制转换为
MainActivity
,因为
MainActivity
类具有
updateVL
方法而不是
context
类,因此编译器将在应用静态绑定时向您显示错误

@JavascriptInterface
public void openLvl(int lvl){
    if(context instanceof MainActivity) // add safety check if required
        ((MainActivity)context).updateLvl(lvl);
}

上下文
不是您的
WebViewJavaScriptInterface2
的实例,而是android系统的一个类(以及活动)

一种方法是按照帕夫尼特的建议将上下文投射到活动中。但是,这有一个缺陷,即您无法绝对确定WebViewJavaScriptInterface2中是否从正确的活动实例化。如果你将它投射到一个活动中,你也可以从一个活动中使用它


更简洁的方法是定义一个回调接口,在您的活动(或多个活动)中实现该接口,并将该回调接口传递到
WebViewJavaScriptInterface2

上下文
不是
WebViewJavaScriptInterface2
的实例,而是android系统的一个类(以及活动)

一种方法是按照Pavneet的建议将上下文强制转换为活动。但这有一个缺陷,即您无法绝对确定WebViewJavaScriptInterface2中的上下文是否是从正确的活动中实例化的。此外,如果您将上下文强制转换为某个活动,您也可以仅从该活动中使用它


一种更简洁的方法是定义回调接口,在活动(或多个活动)中实现该接口,并将该回调接口传递到
WebViewJavaScriptInterface2

这可能会对某些人有所帮助

new Handler(Looper.getMainLooper())
  .post(new Runnable(){
    @override
    public void run(){
      updateLvl(lvl);
    }
  });

这可能对某人有所帮助

new Handler(Looper.getMainLooper())
  .post(new Runnable(){
    @override
    public void run(){
      updateLvl(lvl);
    }
  });