我有一个问题,在模拟器上我的应用程序中的一些东西不起作用。事情是onDownloadListener()
方法。在真实设备中,它运行完美:我可以下载图像并通过信使发送它,例如在模拟器中它不起作用,该方法不会自行运行。这很糟糕,我尝试过不同的模拟器和不同的设备。在我尝试过的每台设备上都可以使用,但没有一个模拟器可以运行此代码:
public class MyExport implements DownloadListener {
private final WebView webView;
private Context context;
private Activity activity;
public MyExport(Context c, Activity a, WebView webView) {
this.activity = a;
this.context = c;
this.webView = webView;
}
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
System.out.println("IM IN onDwoanloadStart");
String intentType = "image/png";
String fileName = "img.png";
try {
if(url != null) {
FileOutputStream fos;
fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
//conversion stuff
fos.write(decodedStr);
fos.getFD().sync();
fos.flush();
fos.close();
Intent sendIntent = new Intent();
sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
sendIntent.setAction(Intent.ACTION_SEND);
File filePath = new File(String.valueOf(context.getFilesDir()));
File newFile = new File(filePath, fileName);
sendIntent.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".FileProvider", newFile));
sendIntent.setType(intentType);
context.startActivity(sendIntent);
}
} catch (IOException e) {
e.printStackTrace();
}
}
@JavascriptInterface
public void runExport(){
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
webView.loadUrl("javascript: obj.exportImage()");
}
});
}
这就是我将其添加到webView
的方式:
MyExport export = new MyExport(activity.getBaseContext(),activity,this.webView);
this.webView.setDownloadListener(出口);
this.webView.addJavascriptInterface(export,“jsint”);
当我点击WebView
中的按钮时,exportImage()
的{{1}}被调用,但Javascript
未被调用。这种情况在模拟器上发生 ONLY !