下面是我的应用程序问题的图片。
启动时的应用程序:
100% 进展:
现在,如果我按回键并再次返回应用程序:
我真正想要的:当用户在按下返回键后返回时,应用程序应该恢复而不是重新加载。
我正在分享下面的代码。请提出解决方案。
package com.url.app;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.webkit.ValueCallback;
import android.webkit.WebView;
import android.webkit.WebChromeClient;
import android.webkit.WebViewClient;
import android.provider.Settings;
public class MainActivity extends Activity {
private WebView wv;
//make HTML upload button work in Webview
private ValueCallback<Uri> mUploadMessage;
private final static int FILECHOOSER_RESULTCODE=1;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if(requestCode==FILECHOOSER_RESULTCODE)
{
if (null == mUploadMessage) return;
Uri result = intent == null || resultCode != RESULT_OK ? null
: intent.getData();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}
}
//Check if user is online
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
return true;
}
return false;
}
//if not online show alertdialog with settings button
public void showNoConnectionDialog(Context ctx1) {
final Context ctx = ctx1;
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setCancelable(true);
builder.setMessage(R.string.no_connection);
builder.setTitle(R.string.no_connection_title);
builder.setPositiveButton(R.string.settings, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ctx.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
return;
}
});
builder.setCancelable(false);
builder.show();
}
//supress lint coz it cries over javascript enabled
@SuppressLint("SetJavaScriptEnabled") @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//if not online then show NO CONNECTION Dialog
if(!isOnline()) {
showNoConnectionDialog(MainActivity.this);
}
//webview
wv = new WebView(this);
//loadurl
wv.loadUrl(getString(R.string.siteurl));
//Enable JavaScript
wv.getSettings().setJavaScriptEnabled(true);
//Zoom
wv.getSettings().setBuiltInZoomControls(true);
wv.getSettings().setSupportZoom(true);
//progressdialog
final ProgressDialog pd = ProgressDialog.show(this, "", getString(R.string.loading), true);
pd.setCanceledOnTouchOutside(false);
wv.setWebViewClient(new WebViewClient(){
//hide progressdialog when page loads completely
@Override
public void onPageFinished(WebView view, String url) {
if(pd.isShowing() && pd!=null)
{
pd.dismiss();
}
}
});
wv.setWebChromeClient(new WebChromeClient() {
// For Android 3.0+
public void openFileChooser( ValueCallback<Uri> uploadMsg, String acceptType ) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
MainActivity.this.startActivityForResult( Intent.createChooser( i, getString(R.string.fileselect) ), MainActivity.FILECHOOSER_RESULTCODE );
}
// For Android < 3.0
public void openFileChooser( ValueCallback<Uri> uploadMsg ) {
openFileChooser( uploadMsg, "" );
}
// For Android > 4.1
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
openFileChooser( uploadMsg, "" );
}
});
setContentView(wv);
}
}
PS:上传按钮在我的代码中工作,问题是我如何在后台保持 WebView 处于活动状态,这样如果用户开始上传并退出应用程序,上传会继续。
更新:我认为在这种情况下覆盖后退按钮和通知可能很有用。