我不是Android或Java Developer,而是全职Web开发人员。.但是我有一个用于制作webviews项目的模板,该模板在大多数情况下都可以使用...但是此登录名无效。.
我有一个网站可以使用facebook Js SDK 3.2版
它可以完美地在上运行:
它可以完美地在上使用:我手机中的Google Chrome
但是在我插入Facebook的用户和密码并按登录后,在webView的APP中冻结了它。 我认为它无法返回上一页。
我在这里读了很多文章,但是没有用。.
这是我的MainActivity:
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.CookieManager;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static final String target_url = "https://myapp.com/gen_age/index.php";
private static final String target_url_prefix = "myapp.com/gen_age/index.php";
private WebView mWebview;
private WebView mWebviewPop;
private FrameLayout mContainer;
private SwipeRefreshLayout swipeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
mWebview = findViewById(R.id.webView);
mContainer = findViewById(R.id.webview_frame);
swipeLayout = findViewById(R.id.swipe_container);
final WebSettings webSettings = mWebview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAppCacheEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setSupportMultipleWindows(true);
mWebview.setWebViewClient(new UriWebViewClient());
mWebview.setWebChromeClient(new UriChromeClient());
mWebview.loadUrl(target_url);
swipeLayout.setRefreshing(true);
swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
//Do your task
mWebview.reload();
}
});
}
private class UriWebViewClient extends WebViewClient {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
swipeLayout.setRefreshing(false);
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
swipeLayout.setRefreshing(false);
}
@Override
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
super.onReceivedHttpError(view, request, errorResponse);
swipeLayout.setRefreshing(false);
}
@Override
public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
try {
String host = Uri.parse(url).getHost();
//Log.d("shouldOverrideUrlLoading", url);
if (host.equals(target_url_prefix)) {
// This is my web site, so do not override; let my WebView load
// the page
if (mWebviewPop != null) {
mWebviewPop.setVisibility(View.GONE);
mContainer.removeView(mWebviewPop);
mWebviewPop = null;
}
return false;
}
if (host.equals("m.facebook.com")) {
return false;
}
} catch (Exception e) {
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();
}
// swipeLayout.setRefreshing(false);
return true;
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
swipeLayout.setRefreshing(false);
Log.d("onReceivedSslError", "onReceivedSslError");
//super.onReceivedSslError(view, handler, error);
}
}
@Override
public void onBackPressed() {
if (mWebview.canGoBack()) {
mWebview.goBack();
} else {
finish();
}
}
class UriChromeClient extends WebChromeClient {
@Override
public boolean onCreateWindow(WebView view, boolean isDialog,
boolean isUserGesture, Message resultMsg) {
mWebviewPop = new WebView(getApplicationContext());
mWebviewPop.setVerticalScrollBarEnabled(false);
mWebviewPop.setHorizontalScrollBarEnabled(false);
mWebviewPop.setWebViewClient(new UriWebViewClient());
mWebviewPop.getSettings().setJavaScriptEnabled(true);
mWebviewPop.getSettings().setSavePassword(false);
mWebviewPop.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
mContainer.addView(mWebviewPop);
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(mWebviewPop);
resultMsg.sendToTarget();
return true;
}
@Override
public void onCloseWindow(WebView window) {
Log.d("onCloseWindow", "called");
}
}
}
答案 0 :(得分:0)
我要在这里走出去,说您正在主UI线程上进行IO操作(例如尝试通过Internet发送凭据登录)。如果是这种情况,这就是导致您的应用程序冻结的原因。
我找不到负责登录的实际代码,但是,如果您要发送HTTP请求或类似的请求,您需要在后台线程中完成,这样用户界面就不会冻结。
Android中的主线程负责更新UI,如果您通过阻止IO操作对其进行阻止,它将冻结。