浏览代码

调转网页类

LiuJie 10 年之前
父节点
当前提交
653550dd1a

+ 167 - 0
WeiChat/src/main/java/com/sk/weichat/ui/erp/activity/WebViewCommActivity.java

@@ -0,0 +1,167 @@
+package com.sk.weichat.ui.erp.activity;
+
+import com.sk.weichat.MyApplication;
+import com.sk.weichat.R;
+import com.sk.weichat.ui.MainActivity;
+import com.sk.weichat.ui.base.BaseActivity;
+
+import org.apache.http.cookie.Cookie;
+
+import com.handmark.pulltorefresh.library.PullToRefreshWebView;
+import com.sk.weichat.ui.erp.util.CommonUtil;
+import com.sk.weichat.ui.erp.util.StringUtils;
+
+import android.annotation.SuppressLint;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+import android.view.View;
+import android.webkit.CookieManager;
+import android.webkit.CookieSyncManager;
+import android.webkit.WebSettings;
+import android.webkit.WebView;
+import android.webkit.WebViewClient;
+import android.widget.ImageView;
+import android.widget.ProgressBar;
+import android.widget.TextView;
+
+/**
+ * Created by Administrator on 2016/4/5.
+ */
+public class WebViewCommActivity extends BaseActivity {
+    private PullToRefreshWebView webView;
+    private ProgressBar pb;
+    private ImageView back;
+    private ImageView refresh;
+    private String url;
+    private TextView title;
+    private TextView tck_icon;
+    private boolean isStartApp = false;
+
+    @SuppressLint("SetJavaScriptEnabled")
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        Intent intent = getIntent();
+        setContentView(R.layout.activity_webview);
+        webView = (PullToRefreshWebView) findViewById(R.id.webView_listview);
+        isStartApp = false;
+        pb = (ProgressBar) findViewById(R.id.pb);
+        pb.setMax(100);
+        url = intent.getStringExtra("url");
+        String p = "";//动态改变文字显示
+        try {
+            p = intent.getStringExtra("p");
+        } catch (Exception e) {
+        }
+        if (p != null) {
+            getSupportActionBar().setTitle(p);
+        }
+        if (StringUtils.isEmpty(url)) {
+            url = "http://www.baidu.com";
+        }
+        System.out.println("webview  url=" + url);
+        webView.getRefreshableView().getSettings().setJavaScriptEnabled(true);
+        webView.getRefreshableView().getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); //设置 缓存模式
+        webView.getRefreshableView().getSettings().setDomStorageEnabled(true);
+        webView.getRefreshableView().getSettings().setDatabaseEnabled(true);
+        webView.getRefreshableView().getSettings().setAppCacheEnabled(true);
+        if (MyApplication.cookie != null) {
+            synCookies(this, url);
+        }else{
+            clearCookie();
+            webView.getRefreshableView().clearHistory();
+            webView.getRefreshableView().clearCache(true);
+        }
+
+        webView.getRefreshableView().loadUrl(url);
+        webView.getRefreshableView().setWebViewClient(new WebViewClient() {
+            public boolean shouldOverrideUrlLoading(WebView view, String url) {
+                if (MyApplication.cookie != null) {
+                    synCookies(WebViewCommActivity.this, url);
+                }
+                view.loadUrl(url);
+                return true;
+            }
+
+        });
+        webView.getRefreshableView().setWebChromeClient(new WebChromeClient());
+
+    }
+
+
+    @Override
+    protected void onResume() {
+        super.onResume();
+//        XGPushClickedResult result=XGPushManager.onActivityStarted(this);
+//        if (result!=null) {
+//            isStartApp=true;
+//            String customParam=result.getCustomContent();
+//            Map<String,Object> pMap=FlexJsonUtil.fromJson(customParam);
+//            title.setText(pMap.get("title").toString());
+//            url=Constants.basicURL+
+//                    "?b_username="+CommonUtil.getSharedPreferences(this, "user_phone")+"&b_password="+
+//                    CommonUtil.getSharedPreferences(this, "user_password")+
+//                    pMap.get("url").toString();
+//            if (MyActivityManager.cookie!=null) {
+//                synCookies(this, url);
+//            }
+//            Log.i("xinge", "onResue url="+url);
+//            webView.getRefreshableView().loadUrl(url);
+//        }
+    }
+
+
+    public class WebChromeClient extends android.webkit.WebChromeClient {
+        @Override
+        public void onProgressChanged(WebView view, int newProgress) {
+
+            pb.setProgress(newProgress);
+            if (newProgress == 100) {
+                pb.setProgress(newProgress);
+                pb.setVisibility(View.GONE);
+                webView.onRefreshComplete();
+            }
+            super.onProgressChanged(view, newProgress);
+        }
+    }
+
+    /**
+     * 同步一下cookie
+     */
+    public void synCookies(Context context, String url) {
+        CookieSyncManager.createInstance(context);
+        CookieManager cookieManager = CookieManager.getInstance();
+        cookieManager.setAcceptCookie(true);
+        cookieManager.removeSessionCookie();//移除
+        Cookie sessionCookie = MyApplication.cookie;
+        String cookieStr = sessionCookie.getName() + "="
+                + sessionCookie.getValue() + "; domain="
+                + sessionCookie.getDomain();
+        System.out.println("webview cookie:" + cookieStr);
+//        if (!StringUtils.isEmpty(CommonUtil.getSharedPreferences(context, "b2b_cookie"))) {
+//            cookieManager.setCookie(url, CommonUtil.getSharedPreferences(context, "b2b_cookie"));//cookies是在HttpClient中获得的cookie
+//        } else {
+            cookieManager.setCookie(url, cookieStr);//cookies是在HttpClient中获得的cookie
+//        }
+        CookieSyncManager.getInstance().sync();
+    }
+
+    public void clearCookie(){
+        CookieManager cookieManager = CookieManager.getInstance();
+        cookieManager.removeSessionCookie();//移除
+        cookieManager.removeAllCookie();
+    }
+
+
+    @Override
+    public void onBackPressed() {
+        if (isStartApp) {
+            Intent intent = new Intent(WebViewCommActivity.this, MainActivity.class);
+            startActivity(intent);
+        } else {
+            super.onBackPressed();
+        }
+    }
+
+}

+ 24 - 0
WeiChat/src/main/res/layout/activity_webview.xml

@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:background="@color/lightgrey"
+    android:orientation="vertical" >
+    <ProgressBar
+        android:id="@+id/pb"
+        style="?android:attr/progressBarStyleHorizontal"
+        android:layout_width="fill_parent"
+        android:layout_height="5dip"
+        android:animationResolution="100"
+        android:indeterminate="false"
+        android:indeterminateDuration="500"
+        android:indeterminateOnly="false"
+        android:max="100"
+        android:progressDrawable="@drawable/webview_progress_bar" >
+    </ProgressBar>
+    <com.handmark.pulltorefresh.library.PullToRefreshWebView
+        android:id="@+id/webView_listview"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:background="@color/linen" />
+</LinearLayout>