覆盖后退按钮以恢复应用

时间:2013-03-30 11:54:56

标签: java android android-webview android-service back-button

以下是我的应用程序问题的图示。

APP启动时:

Snapshot of app while starting

ON 100%进展:

LOADED PAGE

现在,如果我重新打开钥匙并再次返回APP:

LOADS AGAIN

我实际上想要什么:应用程序应该恢复,而不是在用户按下返回键后返回时重新加载。

我正在分享下面的代码。请提出解决方案。

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在后台保持活动状态,这样如果用户开始上传并退出应用程序,则继续上传。

更新:我认为overriding the back button along with a notification在这种情况下可能会有用。

1 个答案:

答案 0 :(得分:0)

捕捉后退按钮的行为类似于主页

 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
     if (keyCode == KeyEvent.KEYCODE_BACK) {
         moveTaskToBack(true);
         return true;
     }
     return super.onKeyDown(keyCode, event);
 }