When develope the web app in Android, you have to customize all of the function by yourself; otherwise, they don't work.
We should implement three classes for handling various actions you need.
When develope the web app in Android, you have to customize all of the function by yourself; otherwise, they don't work.
We should implement three classes for handling various actions you need.
public class MyActivity | |
{ | |
public static ValueCallback<Uri[]> mFilePathCallback; | |
public static Uri imageUri = null; | |
// Get the object from xml file. | |
protected WebView wvClient; | |
@Override | |
public void onCreate(Bundle savedInstanceState) | |
{ | |
super.onCreate(savedInstanceState); | |
// Web view debug. | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) | |
{ | |
WebView.setWebContentsDebuggingEnabled(true); | |
} | |
// Add the Javascript interface for communication between this application and the server. | |
this.wvClient.addJavascriptInterface(MyWebAppInterface, getString(R.string.app_name)); | |
// Setting Javascript can run on the website. | |
this.wvClient.getSettings().setJavaScriptEnabled(true); | |
// This is for playing a media on the webview. | |
this.wvClient.setLayerType(View.LAYER_TYPE_HARDWARE, null); | |
// Set a client for observe web site state. | |
this.wvClient.setWebViewClient(new MyWebViewClient()); | |
// Set a chrome event for catching various events. | |
this.wvClient.setWebChromeClient(new MyWebChromeClient()); | |
// Set a download listener for downloading a file. | |
this.wvClient.setDownloadListener(new MyWebDownloadListener()); | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) | |
{ | |
super.onActivityResult(requestCode, resultCode, data); | |
if (RESULT_OK == resultCode) | |
{ | |
switch (requestCode) | |
{ | |
case 5566: | |
Uri[] results; | |
// If there is not data, then we may have taken a photo | |
if (null == data.getData()) | |
{ | |
results = new Uri[] {imageUri}; | |
} | |
else | |
{ | |
results = new Uri[] {Uri.parse(data.getDataString())}; | |
} | |
mFilePathCallback.onReceiveValue(results); | |
mFilePathCallback = null; | |
break; | |
default: | |
break; | |
} | |
} | |
else if (RESULT_CANCELED == resultCode) | |
{ | |
mFilePathCallback.onReceiveValue(new Uri[] { }); | |
mFilePathCallback = null; | |
} | |
} | |
} |
import android.webkit.DownloadListener; | |
import android.webkit.URLUtil; | |
public class MyDownloadListener implements DownloadListener | |
{ | |
@Override | |
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) | |
{ | |
String filename = URLUtil.guessFileName(url, contentDisposition, mimetype); | |
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); | |
request.allowScanningByMediaScanner(); | |
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); | |
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename); | |
((DownloadManager) tantraView.context().getSystemService(DOWNLOAD_SERVICE)).enqueue(request); | |
tantraView.showToastMsg("Start to download " + filename + "...", Toast.LENGTH_SHORT); | |
} | |
} |
public class MyWebChromeClient extends WebChromeClient | |
{ | |
// From javascript alert() function. | |
@Override | |
public boolean onJsAlert(WebView view, String url, String message, JsResult result) | |
{ | |
new AlertDialog.Builder(context(), R.style.MyAlertDialogStyle).setTitle("title") | |
.setMessage(message) | |
.setPositiveButton("ok", (dialog, which) -> result.confirm()) | |
.setCancelable(false) | |
.create() | |
.show(); | |
return true; | |
} | |
// From javascript confirm() function. | |
@Override | |
public boolean onJsConfirm(WebView view, String url, String message, JsResult result) | |
{ | |
new AlertDialog.Builder(context(), R.style.MyAlertDialogStyle).setTitle("title") | |
.setMessage(message) | |
.setPositiveButton("ok", (dialog, which) -> result.confirm()) | |
.setNegativeButton("cancel", (dialog, which) -> result.cancel()) | |
.setCancelable(false) | |
.create() | |
.show(); | |
return true; | |
} | |
@Override | |
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) | |
{ | |
// Directlt use this callback in anywhere. | |
if (mFilePathCallback != null) | |
{ | |
mFilePathCallback.onReceiveValue(new Uri[] { }); | |
mFilePathCallback = null; | |
} | |
mFilePathCallback = filePathCallback; | |
// Create camera intent for taking a picture. | |
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | |
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg"); | |
if (file.exists()) | |
{ | |
file.delete(); | |
file = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg"); | |
} | |
imageUri = Uri.fromFile(file); | |
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); | |
// Create intent for picking any data type of file. | |
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT); | |
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE); | |
contentSelectionIntent.setType("*/*"); | |
// Create intent for picking a photo from the gallery | |
Intent photosSelectionIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); | |
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER); | |
chooserIntent.putExtra(Intent.EXTRA_TITLE, "Choose an action"); | |
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {takePictureIntent, photosSelectionIntent}); | |
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent); | |
startActivityForResult(chooserIntent, 5566); | |
return true; | |
} | |
} |
public class MyWebViewClient extends WebViewClient | |
{ | |
@Override | |
public boolean shouldOverrideUrlLoading(WebView view, String url) | |
{ | |
return false; | |
} | |
@Override | |
public void onPageStarted(WebView view, String url, Bitmap favicon) | |
{ | |
super.onPageStarted(view, url, favicon); | |
} | |
@Override | |
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) | |
{ | |
super.onReceivedError(view, request, error); | |
} | |
@Override | |
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) | |
{ | |
super.onReceivedHttpError(view, request, errorResponse); | |
} | |
@Override | |
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) | |
{ | |
super.onReceivedSslError(view, handler, error); | |
} | |
@Override | |
public void onPageFinished(WebView view, String url) | |
{ | |
super.onPageFinished(view, url); | |
} | |
} |