android与h5简单交互(js调取android的拨打电话功能)

public class MainActivity extends AppCompatActivity {
    public static final int REQUEST_CALL_PERMISSION = 101; //拨号请求码
    public static final String TAG = "TAG";
    private WebView mWebView;

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

        mWebView = (WebView) findViewById(R.id.webView);
        //加载本地html文件
        mWebView.loadUrl("file:///android_asset/index.html");
        //设置java可以调用js方法
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        //定义JsInteration方法,供js调用
        mWebView.addJavascriptInterface(new JsInteration(), "android");

    }

    //Android调用有返回值js方法
    @TargetApi(Build.VERSION_CODES.KITKAT)
    public void onClick(View v) {
        mWebView.evaluateJavascript("sum(1,2)", new ValueCallback() {
            @Override
            public void onReceiveValue(String value) {
                //Log.e(TAG, "onReceiveValue value=" + value);
                Toast.makeText(MainActivity.this, "android调用js得到的结果是:" + value, Toast.LENGTH_SHORT).show();
            }
        });
    }

    public class JsInteration {
        @JavascriptInterface
        public void back() {
            checkReadPermission(Manifest.permission.CALL_PHONE, REQUEST_CALL_PERMISSION);
            call("tel:" + "10000");
        }
    }

    /**
     * 判断是否有某项权限
     *
     * @param string_permission 权限
     * @param request_code      请求码
     * @return
     */
    public boolean checkReadPermission(String string_permission, int request_code) {
        boolean flag = false;
        if (ContextCompat.checkSelfPermission(this, string_permission) == PackageManager.PERMISSION_GRANTED) {//已有权限
            flag = true;
        } else {//申请权限
            ActivityCompat.requestPermissions(this, new String[]{string_permission}, request_code);
        }
        return flag;
    }


    /**
     * 检查权限后的回调
     *
     * @param requestCode  请求码
     * @param permissions  权限
     * @param grantResults 结果
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CALL_PERMISSION: //拨打电话
                if (permissions.length != 0 && grantResults[0] != PackageManager.PERMISSION_GRANTED) {//失败
                    Toast.makeText(this, "请允许拨号权限后再试", Toast.LENGTH_SHORT).show();
                } else {//成功
                    //call("tel:"+"10086");
                    //Toast.makeText(this, "10000", Toast.LENGTH_SHORT).show();
                    call("tel:" + "10000");
                }
                break;
        }
    }

    /**
     * 拨打电话(直接拨打)
     *
     * @param telPhone 电话
     */
    public void call(String telPhone) {
        if (checkReadPermission(Manifest.permission.CALL_PHONE, REQUEST_CALL_PERMISSION)) {
            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(telPhone));
            startActivity(intent);
        }
    }

}

 

//index.html




    
    






//xml文件




    

你可能感兴趣的:(android与h5简单交互(js调取android的拨打电话功能))