我正在使用webview和添加的webinterface类构建一个android webapp。

这是我的代码:

public class WebAppInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    WebAppInterface(Context c) {
        mContext = c;
    }



    /** Show a toast from the web page */
    @JavascriptInterface
    public void showToast(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }


    public void SendSMS(String msg,String PhoneNumber) {
        Toast.makeText(mContext,
                "sending",
                Toast.LENGTH_LONG).show();

        try {

                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendTextMessage(PhoneNumber, null, msg, null, null);
        } catch(Exception e) {
            Toast.makeText(mContext,
                    "SMS not sent, please try again.",
                    Toast.LENGTH_LONG).show();
            e.printStackTrace();

        }

    }
}


MainActiviy:

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebView myWebView = (WebView) findViewById(R.id.webview);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
        if (checkconnection()) {
            myWebView.loadUrl("file:///android_asset/www/index.html");
        } else {
            Context context = getApplicationContext();
            CharSequence text = "אין חיבור לאינטרנט";
            int duration = Toast.LENGTH_LONG;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
        }


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    public boolean checkconnection() {


        ConnectivityManager connMgr = (ConnectivityManager)
                getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
         return true;
        } else {
            return false;
        }

    }
}


我调用的表单Javascript

Android.showToast('test')
Android.SendSMS('0587070580','test sms')
没有例外。

最佳答案

您在@JavascriptInterface方法上缺少SendSMS()批注。


  从API级别JELLY_BEAN_MR1及更高版本开始,只有显式标记有此批注的方法才可用于Javascript代码。


资料来源:JavascriptInterface

10-08 01:13