Enterplorer SDK集成

android studio 集成文檔

android studio集成方法

1.創(chuàng)建一個android工程

2.通過拷貝添加arr文件到libs

image.png

3.在app文件的build.gradle配置如下

android{
...
defaultConfig{
        ...
      ndk {
            abiFilters "armeabi", "armeabi-v7a", "x86" 
          //輸出指定三種abi體系結(jié)構(gòu)下的so庫
        }
      ...
                    }
...
repositories {
        flatDir {
            dirs 'libs'   
      // aar目錄
        }
    }
...
dependencies{
...
implementation(name: "enterplorer_library", ext: "aar")
    implementation(name: "xwalk_core_library", ext: "aar")
...
}
}

4.Manifest權(quán)限

<uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.USE_FINGERPRINT" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.WRITE_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.SEND_SMS" />

5.項目中的使用

  • 使用前請先配置 app/src/main/assets/config.metadata文件


    image.png
  • 內(nèi)容如下
{"companyName": "you company name", "managerServer": "you manager server address", "managerProxy": ""}

以上詳情,請與PM聯(lián)系,并索要相關(guān)信息

5.1初始化SDK
  • 在application中初始化SDK
XCloudSDKManager.getInstance().init(this);
  • 注意事項:
    • 初始化SDK的過程中,需要獲取本機信息,android SDK 23版本或以上,需要權(quán)限.請修改目標(biāo)版本至22,否側(cè)可能初始化失敗.
targetSdkVersion 22
5.2使用

在使用界面,執(zhí)行如下代碼,進行SDK驗證

XCloudSDKManager.getInstance().authRequest(this, new Callback() {
            @Override
            public void success() {
                mLoadingDialog.dismiss();
                initTabFragment("目標(biāo)url");
            }

            @Override
            public void error(String errorMsg) {
                mLoadingDialog.dismiss();
                Toast.makeText(BrowserActivity.this, errorMsg, Toast.LENGTH_SHORT).show();
            }
        });
  • 如上,initTabFragment("url")方法如下
private void initTabFragment(String url) {
        TabFragment.Builder builder = new TabFragment.Builder();

        // ProxyChangeListener.setProxyConfig("192.168.2.18", 8888, null, new String[]{"oa.yunshipei.com"});
        /**
         * 加載{@link #url}時設(shè)置cookie實現(xiàn)單點登錄
         */
        ArrayList<CookieModel> cookieModelList = new ArrayList<>();
        String cookie = CookieManager.getInstance().getCookie(url);cookieModelList.add(new CookieModel(url, cookie));
        builder.setCookies(cookieModelList);
        /**
         * 加載{@link #url}時設(shè)置cookie實現(xiàn)單點登錄
         * builder.setCookies()
         *
         * //給每個加載的url加參數(shù)作為單點登錄
         * builder.setUrlTokenSSO()
         *
         * //設(shè)置proxy,401茶宵,403認(rèn)證信息须肆;在頁面需要做認(rèn)證時可以在此配置認(rèn)證信息矛紫,在頁面需要認(rèn)證時將自動根據(jù)配置信息自動認(rèn)證將不再彈出認(rèn)證對話框
         * builder.setHttpAuthorizations()
         *
         * //設(shè)置全局運行時版本
         * builder.setGlobalRuntime()
         */
        //設(shè)置webView加載的Url
        builder.setUrl(url);
        //設(shè)置默認(rèn)是否支持雙webView退疫。具體是否支持請詢問適配開發(fā)工程師鹉动。
        builder.setDefault2WebView(false);
        //設(shè)置頁面水印
        builder.setWaterMark(new WaterMarkConfig.Builder().setShowDateTime(true).setWtmTexts(Arrays.asList("云適配", "適配SDK")).build());
        //設(shè)置頁面字體大小
        builder.setTextZoom(100);
        //文檔預(yù)覽  使用永中文檔轉(zhuǎn)換服務(wù)器進行文檔預(yù)覽
        FilePreviewConfig.Builder filePreviewConfigBuilder  = new FilePreviewConfig.Builder();
        //設(shè)置永中文檔轉(zhuǎn)換服務(wù)器地址
        filePreviewConfigBuilder.setDocConvertServer("http://139.217.22.35:8080");

        HashMap<String, String> map = new HashMap<>();
        map.put("rtf", "0");
        map.put("eio", "0");
        map.put("uof", "0");
        map.put("uos", "0");
        map.put("xml", "0");
        map.put("txt", "0");
        filePreviewConfigBuilder.setDocTypes(map);

        /**
        //設(shè)置是否自行處理文檔轉(zhuǎn)換后的url(默認(rèn)狀態(tài)為false,使用SDK內(nèi)部默認(rèn)的文檔在線處理邏輯)
        filePreviewConfigBuilder.setExternalPreview(false);
        //在訪問永中轉(zhuǎn)換服務(wù)器時如果需要通過代理訪問爪喘,請設(shè)置代理服務(wù)信息
        filePreviewConfigBuilder.setFilePreviewServerProxy("xxx.xxx.xxx.xxxx", 8787);
         **/
        builder.setFilePreview(filePreviewConfigBuilder.build());

        /**
         * TabFragment是用來顯示webView窗口的重要組件颜曾。tabFragment 必須在{@link XCloudSDKManager#authRequest(Context, Callback)}
         * 請求成功回調(diào)到{@link Callback#success()}后才可以創(chuàng)建對象并attach到View中。
         */
        mTabFragment = builder.build();
        ArrayList<HttpAuthModel> httpAuthModels = new ArrayList<>();
        httpAuthModels.add(new HttpAuthModel("host","name","psd"));
        builder.setHttpAuthorizations(httpAuthModels);
        //webView 注入native method
        JavascriptExample javascriptExample = new JavascriptExample(this, mTabFragment);
        mTabFragment.addJavascriptInterface(javascriptExample, "APIExample");

        getSupportFragmentManager().beginTransaction().replace(R.id.fl_browser_tab, mTabFragment, mTabFragment.getClass().getName()).commit();

        /**
         * webView(crosswalk)文件下載監(jiān)聽設(shè)置
         */
//        mTabFragment.setEnterDownloadListener(new EnterDownloadListener() {
//            @Override
//            public void onDownload(String fromUrl, XDownloadModel xDownloadModel) {
//                /**
//                 * 實現(xiàn)具體的下載邏輯...
//                 */
//            }
//        });

        /**
         * 日志上傳
         */
        YspLogUtils.uploadLog("小明", new Callback() {
            @Override
            public void success() {

            }

            @Override
            public void error(String errorMsg) {

            }
        });

        //sdk擴展功能
        setextralEvent(mTabFragment);
        mTabFragment.setOnFragmentInteractionListener(new TabFragment.OnFragmentInteractionListener() {

            @Override
            public void backTop() {
                finish();
            }

            @Override
            public void webViewUrlChanged(String s) {
            }

            @Override
            public void kingGridFile(String s) {

            }

            private int retry = 3;
            @Override
            public boolean onReceivedHttpAuthRequest(XWalkView xWalkView, XWalkHttpAuthHandler xWalkHttpAuthHandler, final String host, String s1) {
                String u = YspSharePreUtil.getInstance(BrowserActivity.this).getLocalStorage("YSP_" + host + "_username");
                String p = YspSharePreUtil.getInstance(BrowserActivity.this).getLocalStorage("YSP_" + host + "_password");

                if (retry-- > 0) {
                    xWalkHttpAuthHandler.proceed(u, p);
                    return true;
                }
                final XWalkHttpAuthHandler haHandler = xWalkHttpAuthHandler;
                Context context = xWalkView.getContext();
                LinearLayout layout = new LinearLayout(context);
                final EditText userNameEditText = new EditText(context);
                final EditText passwordEditText = new EditText(context);
                layout.setOrientation(LinearLayout.VERTICAL);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                    layout.setPaddingRelative(10, 0, 10, 20);
                } else {
                    layout.setPadding(10, 0, 10, 20);
                }
                userNameEditText.setHint(R.string.http_auth_user_name);
                passwordEditText.setHint(R.string.http_auth_password);
                passwordEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                layout.addView(userNameEditText);
                layout.addView(passwordEditText);

                AlertDialog.Builder httpAuthDialog = new AlertDialog.Builder(BrowserActivity.this);
                httpAuthDialog.setTitle(R.string.http_auth_title)
                        .setView(layout)
                        .setCancelable(false)
                        .setPositiveButton(R.string.http_auth_log_in,
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int whichButton) {
                                        String userName = userNameEditText.getText().toString();
                                        String password = passwordEditText.getText().toString();
                                        haHandler.proceed(userName, password);
                                        YspSharePreUtil.getInstance(BrowserActivity.this).setLocalStorage("YSP_" + host + "_username", userName);
                                        YspSharePreUtil.getInstance(BrowserActivity.this).setLocalStorage("YSP_" + host + "_password", password);
                                        dialog.dismiss();
                                    }
                                }).setNegativeButton(android.R.string.cancel, null)
                        .setOnCancelListener(new DialogInterface.OnCancelListener() {
                            @Override
                            public void onCancel(DialogInterface dialog) {
                                haHandler.cancel();
                            }
                        }).create().show();
                return true;
            }

            @Override
            public void tabProgressChanged(int i) {

            }

            @Override
            public void backPreWebView(boolean b) {

            }

            @Override
            public boolean shouldHandleNoMatcherUrlFromOpenWindow(String s) {
                return false;
            }

            @Override
            public boolean shouldInterceptWebViewShouldOverrideUrlLoadingMethod(XWalkView xWalkView, String s) {
                return false;
            }

            // 密碼代填
            @Override
            public AutoLoginModel getAutoLoginModel(String s) {
                if (s.contains("http://oa.yunshipei.com/")) {
                    return new AutoLoginModel("http://*[@id=\"userId\"]", "http://*[@id=\"userPwd\"]", "http://*[@id=\"tableheight\"]/tbody/tr[3]/td/table/tbody/tr/td[2]/table/tbody/tr[1]/td[3]/img", "test1", "123456");
                }
                return null;
            }

            /**
             * 永中文檔在線預(yù)覽開發(fā)者自行處理秉剑。
             * @param s   文檔轉(zhuǎn)換后的地址泛豪;如:http://192.xxxx.xxx.xxx/prexxxx.html
             */
            @Override
            public void filePreviewConvertResult(String s) {
                /**
                 * 如果在設(shè)置永中文檔轉(zhuǎn)換服務(wù)器轉(zhuǎn)化后將url暴露出來供開發(fā)者自己處理。
                 * filePreviewConfigBuilder.setExternalPreview(true);
                 * 此時需要開發(fā)者自己處理相關(guān)邏輯
                 */
            }

            /**
             * 自定義WebView(crosswalk)適配頁面加載的loading樣式
             * @param s     加載適配頁面?zhèn)鬟f過來的提示信息侦鹏;
             * @return      true標(biāo)示使用自定義的loading诡曙;false標(biāo)示使用sdk默認(rèn)loading
             */
            @Override
            public boolean showAdapterLoading(String s) {
                if(webLoadingDialog != null) {
                    webLoadingDialog.show();
                    //使用自定義loading樣式時,返回true
                    return true;
                }
                //使用默認(rèn)loading則返回false
                return false;
            }

            /**
             * 關(guān)閉自定義適配頁面加載loading
             * 此方法必須和{@link #showAdapterLoading(String)}同時實現(xiàn)
             * @return  true標(biāo)示關(guān)閉自定義的loading略水;false標(biāo)示關(guān)閉sdk默認(rèn)loading
             */
            @Override
            public boolean hideAdapterLoading() {
                if(webLoadingDialog != null && webLoadingDialog.isShowing()) {
                    webLoadingDialog.cancel();
                    //關(guān)閉自定義loading樣式時价卤,返回true
                    return true;
                }
                //關(guān)閉默認(rèn)的loading,直接返回false
                return false;
            }

            @Override
            public String getSecurityGatewayKey() {
                return null;
            }
        });
    }
5.3 全部代碼如下
package com.example.app;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.CookieManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.example.model.JavascriptExample;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.yunshipei.core.common.Callback;
import com.yunshipei.core.common.PageLocationListener;
import com.yunshipei.core.common.QRCodeDecoderListener;
import com.yunshipei.core.common.bridge.inter.JsCallback;
import com.yunshipei.core.manager.XCloudSDKManager;
import com.yunshipei.core.model.AutoLoginModel;
import com.yunshipei.core.model.CookieModel;
import com.yunshipei.core.model.FilePreviewConfig;
import com.yunshipei.core.model.HttpAuthModel;
import com.yunshipei.core.model.WaterMarkConfig;
import com.yunshipei.core.ui.TabFragment;
import com.yunshipei.core.utils.YspLogUtils;
import com.yunshipei.core.utils.YspSharePreUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.xwalk.core.XWalkHttpAuthHandler;
import org.xwalk.core.XWalkView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BrowserActivity extends AppCompatActivity {
    private static final String TAG = BrowserActivity.class.getSimpleName();
    public static final String EXTRA_URL = "adapter_url";
    public static final String EXTRA_TITLE = "adapter_title";
    public static final String EXTRA_IDS = "adapters_ids";

    private TabFragment mTabFragment;
    private LoadingDialog webLoadingDialog = null;
    private AMapLocationClient aMapLocationClient = null;          //高德定位
    private AMapLocationClientOption option = null;
    private JsCallback scanQRCodeJsCallback = null;         //js二維碼掃描結(jié)果回調(diào)

    @BindView(R.id.tv_title)
    protected TextView tvTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_browser);
        ButterKnife.bind(this);
        tvTitle.setText(getIntent().getStringExtra(EXTRA_TITLE));
        final String url = getIntent().getStringExtra(EXTRA_URL);
        final LoadingDialog mLoadingDialog = new LoadingDialog(this);
        mLoadingDialog.setTitle("加載中渊涝,請稍候...");
        mLoadingDialog.show();
        if (savedInstanceState != null) {
            FragmentManager fm = getSupportFragmentManager();
            Fragment fragment = fm.findFragmentByTag(TabFragment.class.getName());
            if (fragment != null) {
                fm.beginTransaction().remove(fragment).commit();
                fm.executePendingTransactions();
            }
        }
        // XCloudSDKManager.getInstance().shouldReAuthRequest(); 有重新登錄需求的時候需要開啟
        XCloudSDKManager.getInstance().authRequest(this, new Callback() {
            @Override
            public void success() {
                mLoadingDialog.dismiss();
                initTabFragment("目標(biāo)url");
            }

            @Override
            public void error(String errorMsg) {
                mLoadingDialog.dismiss();
                Toast.makeText(BrowserActivity.this, errorMsg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onResumeFragments() {
        super.onResumeFragments();
        if (webLoadingDialog == null) {
            webLoadingDialog = new LoadingDialog(this);
            webLoadingDialog.setTitle("頁面加載中...");
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //js二維碼掃描結(jié)果回傳給webpage
        IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (intentResult != null && !TextUtils.isEmpty(intentResult.getContents())) {
            if (scanQRCodeJsCallback != null)
                scanQRCodeJsCallback.onCallback(intentResult.getContents());
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    private void initTabFragment(String url) {
        TabFragment.Builder builder = new TabFragment.Builder();

        // ProxyChangeListener.setProxyConfig("192.168.2.18", 8888, null, new String[]{"oa.yunshipei.com"});
        /**
         * 加載{@link #url}時設(shè)置cookie實現(xiàn)單點登錄
         */
        ArrayList<CookieModel> cookieModelList = new ArrayList<>();
        String cookie = CookieManager.getInstance().getCookie(url);cookieModelList.add(new CookieModel(url, cookie));
        builder.setCookies(cookieModelList);
        /**
         * 加載{@link #url}時設(shè)置cookie實現(xiàn)單點登錄
         * builder.setCookies()
         *
         * //給每個加載的url加參數(shù)作為單點登錄
         * builder.setUrlTokenSSO()
         *
         * //設(shè)置proxy慎璧,401,403認(rèn)證信息跨释;在頁面需要做認(rèn)證時可以在此配置認(rèn)證信息胸私,在頁面需要認(rèn)證時將自動根據(jù)配置信息自動認(rèn)證將不再彈出認(rèn)證對話框
         * builder.setHttpAuthorizations()
         *
         * //設(shè)置全局運行時版本
         * builder.setGlobalRuntime()
         */
        //設(shè)置webView加載的Url
        builder.setUrl(url);
        //設(shè)置默認(rèn)是否支持雙webView。具體是否支持請詢問適配開發(fā)工程師鳖谈。
        builder.setDefault2WebView(false);
        //設(shè)置頁面水印
        builder.setWaterMark(new WaterMarkConfig.Builder().setShowDateTime(true).setWtmTexts(Arrays.asList("云適配", "適配SDK")).build());
        //設(shè)置頁面字體大小
        builder.setTextZoom(100);
        //文檔預(yù)覽  使用永中文檔轉(zhuǎn)換服務(wù)器進行文檔預(yù)覽
        FilePreviewConfig.Builder filePreviewConfigBuilder  = new FilePreviewConfig.Builder();
        //設(shè)置永中文檔轉(zhuǎn)換服務(wù)器地址
        filePreviewConfigBuilder.setDocConvertServer("http://139.217.22.35:8080");

        HashMap<String, String> map = new HashMap<>();
        map.put("rtf", "0");
        map.put("eio", "0");
        map.put("uof", "0");
        map.put("uos", "0");
        map.put("xml", "0");
        map.put("txt", "0");
        filePreviewConfigBuilder.setDocTypes(map);

        /**
        //設(shè)置是否自行處理文檔轉(zhuǎn)換后的url(默認(rèn)狀態(tài)為false岁疼,使用SDK內(nèi)部默認(rèn)的文檔在線處理邏輯)
        filePreviewConfigBuilder.setExternalPreview(false);
        //在訪問永中轉(zhuǎn)換服務(wù)器時如果需要通過代理訪問,請設(shè)置代理服務(wù)信息
        filePreviewConfigBuilder.setFilePreviewServerProxy("xxx.xxx.xxx.xxxx", 8787);
         **/
        builder.setFilePreview(filePreviewConfigBuilder.build());

        /**
         * TabFragment是用來顯示webView窗口的重要組件缆娃。tabFragment 必須在{@link XCloudSDKManager#authRequest(Context, Callback)}
         * 請求成功回調(diào)到{@link Callback#success()}后才可以創(chuàng)建對象并attach到View中捷绒。
         */
        mTabFragment = builder.build();
        ArrayList<HttpAuthModel> httpAuthModels = new ArrayList<>();
        httpAuthModels.add(new HttpAuthModel("host","name","psd"));
        builder.setHttpAuthorizations(httpAuthModels);
        //webView 注入native method
        JavascriptExample javascriptExample = new JavascriptExample(this, mTabFragment);
        mTabFragment.addJavascriptInterface(javascriptExample, "APIExample");

        getSupportFragmentManager().beginTransaction().replace(R.id.fl_browser_tab, mTabFragment, mTabFragment.getClass().getName()).commit();

        /**
         * webView(crosswalk)文件下載監(jiān)聽設(shè)置
         */
//        mTabFragment.setEnterDownloadListener(new EnterDownloadListener() {
//            @Override
//            public void onDownload(String fromUrl, XDownloadModel xDownloadModel) {
//                /**
//                 * 實現(xiàn)具體的下載邏輯...
//                 */
//            }
//        });

        /**
         * 日志上傳
         */
        YspLogUtils.uploadLog("小明", new Callback() {
            @Override
            public void success() {

            }

            @Override
            public void error(String errorMsg) {

            }
        });

        //sdk擴展功能
        setextralEvent(mTabFragment);
        mTabFragment.setOnFragmentInteractionListener(new TabFragment.OnFragmentInteractionListener() {

            @Override
            public void backTop() {
                finish();
            }

            @Override
            public void webViewUrlChanged(String s) {
            }

            @Override
            public void kingGridFile(String s) {

            }

            private int retry = 3;
            @Override
            public boolean onReceivedHttpAuthRequest(XWalkView xWalkView, XWalkHttpAuthHandler xWalkHttpAuthHandler, final String host, String s1) {
                String u = YspSharePreUtil.getInstance(BrowserActivity.this).getLocalStorage("YSP_" + host + "_username");
                String p = YspSharePreUtil.getInstance(BrowserActivity.this).getLocalStorage("YSP_" + host + "_password");

                if (retry-- > 0) {
                    xWalkHttpAuthHandler.proceed(u, p);
                    return true;
                }
                final XWalkHttpAuthHandler haHandler = xWalkHttpAuthHandler;
                Context context = xWalkView.getContext();
                LinearLayout layout = new LinearLayout(context);
                final EditText userNameEditText = new EditText(context);
                final EditText passwordEditText = new EditText(context);
                layout.setOrientation(LinearLayout.VERTICAL);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                    layout.setPaddingRelative(10, 0, 10, 20);
                } else {
                    layout.setPadding(10, 0, 10, 20);
                }
                userNameEditText.setHint(R.string.http_auth_user_name);
                passwordEditText.setHint(R.string.http_auth_password);
                passwordEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                layout.addView(userNameEditText);
                layout.addView(passwordEditText);

                AlertDialog.Builder httpAuthDialog = new AlertDialog.Builder(BrowserActivity.this);
                httpAuthDialog.setTitle(R.string.http_auth_title)
                        .setView(layout)
                        .setCancelable(false)
                        .setPositiveButton(R.string.http_auth_log_in,
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int whichButton) {
                                        String userName = userNameEditText.getText().toString();
                                        String password = passwordEditText.getText().toString();
                                        haHandler.proceed(userName, password);
                                        YspSharePreUtil.getInstance(BrowserActivity.this).setLocalStorage("YSP_" + host + "_username", userName);
                                        YspSharePreUtil.getInstance(BrowserActivity.this).setLocalStorage("YSP_" + host + "_password", password);
                                        dialog.dismiss();
                                    }
                                }).setNegativeButton(android.R.string.cancel, null)
                        .setOnCancelListener(new DialogInterface.OnCancelListener() {
                            @Override
                            public void onCancel(DialogInterface dialog) {
                                haHandler.cancel();
                            }
                        }).create().show();
                return true;
            }

            @Override
            public void tabProgressChanged(int i) {

            }

            @Override
            public void backPreWebView(boolean b) {

            }

            @Override
            public boolean shouldHandleNoMatcherUrlFromOpenWindow(String s) {
                return false;
            }

            @Override
            public boolean shouldInterceptWebViewShouldOverrideUrlLoadingMethod(XWalkView xWalkView, String s) {
                return false;
            }

            // 密碼代填
            @Override
            public AutoLoginModel getAutoLoginModel(String s) {
                if (s.contains("http://oa.yunshipei.com/")) {
                    return new AutoLoginModel("http://*[@id=\"userId\"]", "http://*[@id=\"userPwd\"]", "http://*[@id=\"tableheight\"]/tbody/tr[3]/td/table/tbody/tr/td[2]/table/tbody/tr[1]/td[3]/img", "test1", "123456");
                }
                return null;
            }

            /**
             * 永中文檔在線預(yù)覽開發(fā)者自行處理。
             * @param s   文檔轉(zhuǎn)換后的地址贯要;如:http://192.xxxx.xxx.xxx/prexxxx.html
             */
            @Override
            public void filePreviewConvertResult(String s) {
                /**
                 * 如果在設(shè)置永中文檔轉(zhuǎn)換服務(wù)器轉(zhuǎn)化后將url暴露出來供開發(fā)者自己處理暖侨。
                 * filePreviewConfigBuilder.setExternalPreview(true);
                 * 此時需要開發(fā)者自己處理相關(guān)邏輯
                 */
            }

            /**
             * 自定義WebView(crosswalk)適配頁面加載的loading樣式
             * @param s     加載適配頁面?zhèn)鬟f過來的提示信息;
             * @return      true標(biāo)示使用自定義的loading郭毕;false標(biāo)示使用sdk默認(rèn)loading
             */
            @Override
            public boolean showAdapterLoading(String s) {
                if(webLoadingDialog != null) {
                    webLoadingDialog.show();
                    //使用自定義loading樣式時它碎,返回true
                    return true;
                }
                //使用默認(rèn)loading則返回false
                return false;
            }

            /**
             * 關(guān)閉自定義適配頁面加載loading
             * 此方法必須和{@link #showAdapterLoading(String)}同時實現(xiàn)
             * @return  true標(biāo)示關(guān)閉自定義的loading;false標(biāo)示關(guān)閉sdk默認(rèn)loading
             */
            @Override
            public boolean hideAdapterLoading() {
                if(webLoadingDialog != null && webLoadingDialog.isShowing()) {
                    webLoadingDialog.cancel();
                    //關(guān)閉自定義loading樣式時显押,返回true
                    return true;
                }
                //關(guān)閉默認(rèn)的loading扳肛,直接返回false
                return false;
            }

            @Override
            public String getSecurityGatewayKey() {
                return null;
            }
        });
    }

    @OnClick({R.id.iv_close, R.id.iv_refresh})
    protected void onClickEvent(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                finish();
                break;
            case R.id.iv_refresh:
                if (mTabFragment != null) {
                    mTabFragment.refresh();
                }
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && mTabFragment != null) {
            //適配頁面的返回邏輯
            mTabFragment.onTabBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mTabFragment != null) {
            mTabFragment.release();
        }

    }

    /**
     * ----------------------------------------------
     * 這部分功能是sdk的相關(guān)擴展功能,如果不需要無需關(guān)注
     * 如果需要使用相關(guān)方法可以根據(jù)具體需求具體實現(xiàn)
     * 一下實現(xiàn)僅為接口調(diào)用方法展示
     *
     * @param tabFragment
     */
    private void setextralEvent(TabFragment tabFragment) {
        if (tabFragment == null) {
            Log.d(TAG, "tabFragment 不能為 null 乘碑!");
            return;
        }
        /**
         * 設(shè)置網(wǎng)頁調(diào)取二維碼掃描事件的監(jiān)聽
         */
        tabFragment.setQrCodeDecoderListener(new QRCodeDecoderListener() {
            @Override
            public void scanQRCode(JsCallback jsCallback) {
                scanQRCodeJsCallback = jsCallback;
                new IntentIntegrator(BrowserActivity.this).initiateScan();
            }

        });

        /**
         * 設(shè)置網(wǎng)頁獲取位置信息的監(jiān)聽
         */
        tabFragment.setPageLocationListener(pageLocationListener);
    }

    private YSPPageLocationListener pageLocationListener = new YSPPageLocationListener();

    class YSPPageLocationListener implements PageLocationListener {

        private JsCallback jsCallback;

        @Override
        public void locationBegin(final JsCallback jsCallback) {
            this.jsCallback = jsCallback;
            if (ActivityCompat.checkSelfPermission(BrowserActivity.this, "android.permission.ACCESS_COARSE_LOCATION")
                    != PackageManager.PERMISSION_GRANTED){
                Toast.makeText(getApplicationContext(),"沒有權(quán)限,請手動開啟定位權(quán)限", Toast.LENGTH_SHORT).show();
                // 申請一個(或多個)權(quán)限塔次,并提供用于回調(diào)返回的獲取碼(用戶定義)
                ActivityCompat.requestPermissions(BrowserActivity.this,new String[]{"android.permission.ACCESS_COARSE_LOCATION"}, 100);
            }else{
                System.out.println("定位開始O爬摺?恪!P髋住!5缳鳌4甭搿!<夥伞V⒏薄!UU晗场!>诿鳌辕坝!");
                doLocation();
            }

        }

        public void doLocation() {
            if (aMapLocationClient == null)
                aMapLocationClient = new AMapLocationClient(BrowserActivity.this);
            if (option == null)
                option = getDefaultOption();
            option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
            aMapLocationClient.setLocationListener(new AMapLocationListener() {
                @Override
                public void onLocationChanged(AMapLocation aMapLocation) {
                    //定位成功
                    if (aMapLocation != null && aMapLocation.getErrorCode() == 0) {
                        JSONObject jsonObject = new JSONObject();
                        try {
                            jsonObject.put("longitude", aMapLocation.getLongitude());
                            jsonObject.put("latitude", aMapLocation.getLatitude());
                            jsonObject.put("address", aMapLocation.getAddress());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        jsCallback.onCallback(jsonObject.toString());

                    } else {
                        //定位失敗
                        StringBuffer sb = new StringBuffer();
                        sb.append("定位失敗" + "\n");
                        sb.append("錯誤碼:" + aMapLocation.getErrorCode() + "\n");
                        sb.append("錯誤信息:" + aMapLocation.getErrorInfo() + "\n");
                        sb.append("錯誤描述:" + aMapLocation.getLocationDetail() + "\n");
                        jsCallback.onCallback(sb.toString() + "");
                    }
                    stopLocation();
                    destroyLocation();
                }
            });
            aMapLocationClient.setLocationOption(option);
            aMapLocationClient.startLocation();
        }
    };


    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            // requestCode即所聲明的權(quán)限獲取碼,在checkSelfPermission時傳入
            case 100:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // 獲取到權(quán)限荐健,作相應(yīng)處理(調(diào)用定位SDK應(yīng)當(dāng)確保相關(guān)權(quán)限均被授權(quán)酱畅,否則可能引起定位失敗)
                    pageLocationListener.doLocation();
                } else {
                    // 沒有獲取到權(quán)限江场,做特殊處理
                    Toast.makeText(getApplicationContext(), "獲取位置權(quán)限失敗圣贸,請手動開啟", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                break;
        }
    }


    /**
     * 定位配置
     *
     * @return
     */
    private AMapLocationClientOption getDefaultOption() {
        AMapLocationClientOption mOption = new AMapLocationClientOption();
        mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//可選,設(shè)置定位模式扛稽,可選的模式有高精度、僅設(shè)備滑负、僅網(wǎng)絡(luò)在张。默認(rèn)為高精度模式
        mOption.setGpsFirst(false);//可選,設(shè)置是否gps優(yōu)先矮慕,只在高精度模式下有效帮匾。默認(rèn)關(guān)閉
        mOption.setHttpTimeOut(30000);//可選,設(shè)置網(wǎng)絡(luò)請求超時時間痴鳄。默認(rèn)為30秒瘟斜。在僅設(shè)備模式下無效
        mOption.setInterval(2000);//可選,設(shè)置定位間隔痪寻。默認(rèn)為2秒
        mOption.setNeedAddress(true);//可選螺句,設(shè)置是否返回逆地理地址信息。默認(rèn)是true
        mOption.setOnceLocation(false);//可選橡类,設(shè)置是否單次定位蛇尚。默認(rèn)是false
        mOption.setOnceLocationLatest(false);//可選,設(shè)置是否等待wifi刷新顾画,默認(rèn)為false.如果設(shè)置為true,會自動變?yōu)閱未味ㄎ蝗〗伲掷m(xù)定位時不要使用
        AMapLocationClientOption.setLocationProtocol(AMapLocationClientOption.AMapLocationProtocol.HTTP);//可選匆笤, 設(shè)置網(wǎng)絡(luò)請求的協(xié)議∑仔埃可選HTTP或者HTTPS炮捧。默認(rèn)為HTTP
        mOption.setSensorEnable(false);//可選,設(shè)置是否使用傳感器惦银。默認(rèn)是false
        mOption.setWifiScan(true); //可選咆课,設(shè)置是否開啟wifi掃描。默認(rèn)為true璧函,如果設(shè)置為false會同時停止主動刷新傀蚌,停止以后完全依賴于系統(tǒng)刷新,定位位置可能存在誤差
        mOption.setLocationCacheEnable(true); //可選蘸吓,設(shè)置是否使用緩存定位善炫,默認(rèn)為true
        return mOption;
    }

    /**
     * 停止定位
     */
    private void stopLocation() {
        if (aMapLocationClient != null)
            aMapLocationClient.stopLocation();
    }

    /**
     * 銷毀定位
     */
    private void destroyLocation() {
        if (aMapLocationClient != null)
            aMapLocationClient.onDestroy();
        aMapLocationClient = null;
    }
}
  • 請注意相機/相冊等回調(diào)
  • 請參考書寫物理返回鍵攔截方法
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市库继,隨后出現(xiàn)的幾起案子箩艺,更是在濱河造成了極大的恐慌,老刑警劉巖宪萄,帶你破解...
    沈念sama閱讀 219,039評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件艺谆,死亡現(xiàn)場離奇詭異,居然都是意外死亡拜英,警方通過查閱死者的電腦和手機静汤,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來居凶,“玉大人虫给,你說我怎么就攤上這事∠辣蹋” “怎么了抹估?”我有些...
    開封第一講書人閱讀 165,417評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長弄兜。 經(jīng)常有香客問我药蜻,道長,這世上最難降的妖魔是什么替饿? 我笑而不...
    開封第一講書人閱讀 58,868評論 1 295
  • 正文 為了忘掉前任语泽,我火速辦了婚禮,結(jié)果婚禮上视卢,老公的妹妹穿的比我還像新娘湿弦。我一直安慰自己,他們只是感情好腾夯,可當(dāng)我...
    茶點故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布颊埃。 她就那樣靜靜地躺著蔬充,像睡著了一般。 火紅的嫁衣襯著肌膚如雪班利。 梳的紋絲不亂的頭發(fā)上饥漫,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天,我揣著相機與錄音罗标,去河邊找鬼庸队。 笑死,一個胖子當(dāng)著我的面吹牛闯割,可吹牛的內(nèi)容都是我干的彻消。 我是一名探鬼主播,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼宙拉,長吁一口氣:“原來是場噩夢啊……” “哼宾尚!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起谢澈,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤煌贴,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后锥忿,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體牛郑,經(jīng)...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年敬鬓,在試婚紗的時候發(fā)現(xiàn)自己被綠了淹朋。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,102評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡钉答,死狀恐怖瑞你,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情希痴,我是刑警寧澤,帶...
    沈念sama閱讀 35,790評論 5 346
  • 正文 年R本政府宣布春感,位于F島的核電站砌创,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏鲫懒。R本人自食惡果不足惜嫩实,卻給世界環(huán)境...
    茶點故事閱讀 41,442評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望窥岩。 院中可真熱鬧甲献,春花似錦、人聲如沸颂翼。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至球及,卻和暖如春氧骤,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背吃引。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評論 1 272
  • 我被黑心中介騙來泰國打工筹陵, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人镊尺。 一個月前我還...
    沈念sama閱讀 48,332評論 3 373
  • 正文 我出身青樓朦佩,卻偏偏與公主長得像,于是被迫代替她去往敵國和親庐氮。 傳聞我的和親對象是個殘疾皇子语稠,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,044評論 2 355

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,167評論 25 707
  • 用兩張圖告訴你,為什么你的 App 會卡頓? - Android - 掘金 Cover 有什么料旭愧? 從這篇文章中你...
    hw1212閱讀 12,732評論 2 59
  • 點擊查看原文 Web SDK 開發(fā)手冊 SDK 概述 網(wǎng)易云信 SDK 為 Web 應(yīng)用提供一個完善的 IM 系統(tǒng)...
    layjoy閱讀 13,768評論 0 15
  • 作者:四九老牛颅筋。我的父親,49年生输枯,屬牛议泵。出生在上海,58年隨母之內(nèi)到合肥桃熄,后下放至徽州地區(qū)先口。從小我就很喜歡聽我父...
    有喵閱讀 934評論 0 2
  • 有時候,孩子是會給你帶來不一樣的驚喜的瞳收。 昨天下午去談事情了碉京,回到家時已經(jīng)是晚上七點多了。在小區(qū)轉(zhuǎn)悠了一圈螟深,也...
    木子米拉閱讀 277評論 0 0