0

我有一个简单的 android 应用程序,它打开一个 webview 来调用一个 web 应用程序。
当 Web 应用程序有 window.close(); 时,我想关闭整个 android 应用程序;

我还希望后退键在网页中导航回来,这是有效的。

窗口正在关闭,但应用程序未关闭且日志未更新。我认为由于某种原因没有触发 onWindowClose。

这是我的安卓应用——

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;

import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {

protected static final String TAG = null;
private WebView webView;


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(event.getAction() == KeyEvent.ACTION_DOWN){
        switch(keyCode)
        {
        case KeyEvent.KEYCODE_BACK:
            if(webView.canGoBack()){
                webView.goBack();
            }else{
                finish();
            }
            return true;
        }

    }
    return super.onKeyDown(keyCode, event);
}

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Map<String,String> extraHeaders = new HashMap<String, String>();
    extraHeaders.put("X-Requested-With", "MY-App");


    //webview use to call own site
    webView = (WebView) findViewById(R.id.webView1);
    webView.setWebViewClient(new WebViewClient()); //use to hide the address bar      
    webView .getSettings().setJavaScriptEnabled(true);     
    webView .getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webView .getSettings().setDomStorageEnabled(true); //to store history  
    webView.setWebViewClient(new WebViewClient()); 
    webView.loadUrl("http://www.Google.com",extraHeaders); 

    WebChromeClient wbc = new WebChromeClient(){


        public void onCloseWindow(WebView webView){
            super.onCloseWindow(webView);

            Log.d(TAG, "Window trying to close");
            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
            finish();

        }

  };


  webView.setWebChromeClient(wbc);

 ;


 } 

这是 Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.google"
android:versionCode="1"
android:versionName="1.0" 
<uses-sdk
  android:minSdkVersion="8"
  android:targetSdkVersion="16" />

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/Google"
    android:theme="@style/AppTheme" 
    android:noHistory="true"
    android:excludeFromRecents="true"
     >

    <activity
        android:name="com.example.google.MainActivity"
        android:label="@string/app_name" 
        android:clearTaskOnLaunch="true"
        android:configChanges="orientation|keyboard|keyboardHidden|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
  </activity>
  </application>

</manifest>'

感谢你的帮助!

4

1 回答 1

0

好的。我做了一些研究,发现某些浏览器禁用了 window.close()。当人们退出网站时,我真的只想关闭应用程序。

在用于注销的网站 php 中,我添加了这个。

// return to main page

  header('Location: https://www.mainpage.com/');

然后我将 MainActivity.Java 更改为:

package com.example.mainpage;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.util.Log;
import android.view.KeyEvent;


import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

    protected static final String TAG = null;
    private WebView webView;


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(event.getAction() == KeyEvent.ACTION_DOWN){
            switch(keyCode)
            {
            case KeyEvent.KEYCODE_BACK:
                if(webView.canGoBack()){
              //    Log.d(TAG, "Before Go Back");
                    webView.goBack();
                }else{
               //   Log.d(TAG, "Before Finish");
                    finish();
                }
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    public class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.equals("https://www.mainpage.com/")) {
            //  Log.d(TAG, "Override");
            //  Log.d("TAG", url);
            //  Log.d(TAG, "Logging Out");
            //  Log.d(TAG, "Before Finish");
              finish();
              return true;
            }
            else {
            // Otherwise, give the default behavior (open in browser)
            //Log.d(TAG, "Do Not Override");
            //Log.d("TAG", url);
           // Log.d(TAG, "loading page");
            return false;
            }
        }

    }

public void onCreate(Bundle savedInstanceState) {

    Log.d(TAG, "In On Create");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Map<String,String> extraHeaders = new HashMap<String, String>();
    extraHeaders.put("X-Requested-With", "MY-App");

    webView = (WebView) findViewById(R.id.webView1);
    webView .getSettings().setJavaScriptEnabled(true);     
    webView .getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webView .getSettings().setDomStorageEnabled(true); //to store history  
    webView.loadUrl("https://www.mainpage.com/Login.php",extraHeaders);              
    webView.setWebViewClient(new MyWebViewClient());    

  };

}
于 2014-11-08T19:53:42.043 回答