问题:
我直接在nativeAction的方法下吊起一个下载方法,导致了下载回调
start(DownloadObservable o, final String url)函数中无论对
H5ReportBean h5Data = downloadData.get(url);的h5Data怎么修改,在其它函数(success、fail、update)都不起作用,get出的数据仍是修改前的数据
private Map<String, H5ReportBean> downloadData = new HashMap<>();
@JavascriptInterface public void nativeAction(final String json) { final H5ReportBean bean = H5DataParse.convert(json); DownloadServices.getInstance().download(bean.getDownloadUrl(), getContext(), bean.getAppName()); if (!downloadData.containsKey(bean.getDownloadUrl())){ downloadData.put(bean.getDownloadUrl(), bean); } }
new DownloadObserver() { @Override public void start(DownloadObservable o, final String url) { H5ReportBean h5Data = downloadData.get(url); if (null != h5Data) { int id = (int) System.currentTimeMillis(); h5Data.setTmpNotifactionId(id); NotificationUtils.sendNotification(getContext(), id, h5Data.getAppName(), "开始下载..."); } } @Override public void success(DownloadObservable o, final String url) { H5ReportBean h5Data = downloadData.get(url); if (null != h5Data && null != h5Data.getTmpNotifactionId()) { Integer id = h5Data.getTmpNotifactionId(); NotificationUtils.sendNotification(getContext(), id, h5Data.getAppName(), "下载成功"); downloadData.remove(url); } } @Override public void fail(DownloadObservable o, final String url) { H5ReportBean h5Data = downloadData.get(url); if (null != h5Data && null != h5Data.getTmpNotifactionId()) { Integer id = h5Data.getTmpNotifactionId(); NotificationUtils.sendNotification(getContext(), id, h5Data.getAppName(), "下载失败"); downloadData.remove(url); } } @Override public void update(DownloadObservable o, final String url, final int progress) { H5ReportBean h5Data = downloadData.get(url); if (null != h5Data&&null!=h5Data.getTmpNotifactionId()) { Integer id = h5Data.getTmpNotifactionId(); NotificationUtils.sendNotification(getContext(), id, h5Data.getAppName(), progress + "%"); } } }; DownloadServices.getInstance().addObservers(mObserver);
通过打印线程id
android.os.Process.myTid()
来判断自己的方法在哪个线程中
无脑的方法可以这样使用
/** * 这个不是UI线程,这里定义的数据,主线程不会共享 * * @param json */ @JavascriptInterface public void nativeAction(final String json) { // h5Action(tjson); runOnUiThread(new Runnable() { @Override public void run() { h5Action(tjson); } }); }