android 与 js 交互实例

    // 下载图片到本地
    @android.webkit.JavascriptInterface
    public void getImg(final String imgUrl) {
        loadingDialog = new BjrsProgressDialog(activity);
        loadingDialog.show();
        File sdCard = Environment.getExternalStorageDirectory();
        File directory_GetImg = new File(sdCard, "image");
        final String savepath = directory_GetImg + "/" + DataDispose.getStringMD5(imgUrl) + ".jpg";
        new Thread(new Runnable() {
            @Override
            public void run() {
                final FileLoader fl = new FileLoader();
                File f = new File(savepath);
                if (fl.loadNetFile(imgUrl, savepath)) {

                    // 延时操作
                    TimerTask task = new TimerTask() {
                        @Override
                        public void run() {
                            if (loadingDialog != null && loadingDialog.isShowing()) {
                                Log.d("下载提示(子线程中)", "加载框关闭了");
                                loadingDialog.dismiss();
                            }
                            activity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    File f = new File(savepath);
                                    if (f.exists() || f.length() != 0) {
                                        Toast.makeText(activity, "图片已保存到->" + savepath, Toast.LENGTH_SHORT).show();
                                    } else {
                                        Toast.makeText(activity, "图片已保存到->" + savepath, Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                        }
                    };
                    Timer timer = new Timer();
                    timer.schedule(task, 1000);//1秒后执行TimeTask的run方法

                    Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(f));
                    activity.sendBroadcast(intent);
                }
            }
        }).start();
    }

    // 分享考试结果
    @android.webkit.JavascriptInterface
    public void share(final String titletextStr, final String sharecontent,
                      final String shareImgUrl, final String shareUrl) {
        loadingDialog = new BjrsProgressDialog(activity);
        loadingDialog.show();
        ImageLoader.loadImage(shareImgUrl, ApplicationApp.savePath + "/img/" + DataDispose.getStringMD5(shareImgUrl), new ImageLoader.ImageCallBack() {
            @Override
            public void imageLoaded(boolean loadSuccess, String imageUrl, Bitmap bitmap) {
                if (loadingDialog != null && loadingDialog.isShowing())
                    loadingDialog.dismiss();
                shareBitmap = bitmap;
                Log.d("分享操作", "进入了分享子线程");
                Log.d("分享操作 Url", "loadSuccess=" + loadSuccess + "  " + shareImgUrl);
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        // 打开分享提示框
                        ApplicationApp.shareSDK.setShareContent(titletextStr, sharecontent, titletextStr,
                                shareUrl, shareImgUrl, shareBitmap, R.drawable.ic_launcher128);
                        ApplicationApp.shareSDK.adddialog_share1(activity, BaseDialog.STYLE_BOTTOM_TRAN_DARK, titletextStr);


                    }
                });
            }
        });
        Log.d("分享操作", "点击了分享");


    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值