android从网络界面发送短信



我正在使用webview构建一个android网络应用程序,并添加了webinterface类。

这是我的代码:

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

主要活动:

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')<===========不工作

没有出现任何异常。

您的SendSMS()方法缺少@JavascriptInterface注释。

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

来源:JavascriptInterface

最新更新