android从网络界面发送短信

时间:2015-03-04 10:25:04

标签: android sms

我正在使用webview构建一个android webapp并添加了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();

        }

    }
}

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

没有例外。

1 个答案:

答案 0 :(得分:1)

您错过了@JavascriptInterface方法的SendSMS()注释。

  

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

来源:JavascriptInterface