AndroidはwebviewでDcloud 5+SDKを統合して携帯データを元の画面に移動させることを実現します.

44757 ワード

需要:manactivityにFragmentを二つ入れます. 一つはwebviewの展示で、二つのFragmentの切り替えをサポートします.
一、Dcloud SDKをダウンロードする.
住所:http://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/80
Androidの原生マンナティとfragmentは書いていません. 
二、
jarファイルをインポート そして依存を追加します.ファイルはsdkのlibsフォルダにあります. 
導入が必要なものは
gallry.jar my-ingeloader.jar.native.jar nioldandroids-24.0.jar pdr.jar.jar.jar.jar
三、もう一つのfragmentのファイルから初期化して展示する.
1.oncreatを書き換える 方法:rootviewを作成する webviewを追加するために使用します.
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i("ceshi","fragment.....1");
    rootView = new FrameLayout(getActivity());
}
2.class傍受類の編纂実現  ICore.IcoreStation Listen
class WebviewModeListener implements ICore.ICoreStatusListener {

    LinearLayout btns = null;
    Activity activity = null;
    ViewGroup mRootView = null;
    IWebview webview = null;
    ProgressDialog pd = null;
    public WebviewModeListener(Activity activity, ViewGroup rootView) {
        this.activity = activity;
        mRootView = rootView;
        btns = new LinearLayout(activity);
        mRootView.setBackgroundColor(0xffffffff);
        mRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
               
                webview.onRootViewGlobalLayout(mRootView);
            }
        });
    }

    /**
     * 5+          
     * */
    @Override
    public void onCoreInitEnd(ICore coreHandler) {
        //         appid
        String appid = "TestAppid";
        //         String url = "file:///android_asset/ceshi.html";//      
        
        webview = SDK.createWebview(activity, url, appid, new IWebviewStateListener() {
            @Override
            public Object onCallBack(int pType, Object pArgs) {
                switch (pType) {
                    case IWebviewStateListener.ON_WEBVIEW_READY: Log.i("ceshi","     +onCallback");
                        //         webview    View ,         ,    webviewhtml        
                        ((IWebview) pArgs).obtainFrameView().obtainMainView().setVisibility(View.INVISIBLE);
                        SDK.attach(mRootView, ((IWebview) pArgs));
                        break;
                    case IWebviewStateListener.ON_PAGE_STARTED: Log.i("ceshi","     +onCallback");
                        //          
                        break;
                    case IWebviewStateListener.ON_PROGRESS_CHANGED: Log.i("ceshi","     +onCallback");
                        //          
                        break;
                    case IWebviewStateListener.ON_PAGE_FINISHED:
                        // webview
                        webview.obtainFrameView().obtainMainView().setVisibility(View.VISIBLE);
                        break;
                }
                return null;
            }
        });

        final WebView webviewInstance = webview.obtainWebview();
        //      
        webviewInstance.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    if (webviewInstance.canGoBack()) {
                        webviewInstance.goBack();
                        return true;
                    }
                }
                return false;
            }
        });
    }

    // 5+SDK         
    @Override
    public void onCoreReady(ICore coreHandler) {
        try {
            //    5+ SDK            // 5+SDK        SDK        
            SDK.initSDK(coreHandler);
            //          5+API
            SDK.requestAllFeature();
           
        } catch (Exception e) {
            Log.i("ceshi","  ");
            e.printStackTrace();
        }
       
    }

    /**
     *     //              
     * private void regNewApi() {
     *       //      js    
     *    String featureName = "T";
     *    //          
     *    String className = "com.HBuilder.integrate.webview.WebViewMode_FeatureImpl";
     *    //      JS      
     *    String content = "(function(plus){function test(){return plus.bridge.execSync('T','test',[arguments]);}plus.T = {test:test};})(window.plus);";
     *    //  5+SDK      
     *    SDK.registerJsApi(featureName, className, content);
     * }
     **/

    @Override
    public boolean onCoreStop() {
        // TODO Auto-generated method stub
        return false;
    }
}
3.oncreatviewの書き換え方法、
 @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        View rootview=inflater.inflate(R.layout.fragment_2,container,false);

        //   5+        
        WebviewModeListener wm = new WebviewModeListener(getActivity(), rootView);
        //    5+  
        mEntryProxy = EntryProxy.init(getActivity(), wm);
        //   5+  
        mEntryProxy.onCreate(getActivity(), savedInstanceState, SDK.IntegratedMode.WEBVIEW, null);
        Log.i("ceshi","fragment.....2");

        return rootView;
    }
3.fragmentで書き換える方法は、不要なものは書かなくてもいいです.
 
@Override
public void onDestroy() {
    super.onDestroy();
    
    mEntryProxy.onStop(getActivity());
}

@Override
public void onResume() {
    super.onResume();
    mEntryProxy.onResume(getActivity());
  
}

@Override
public void onPause() {
    super.onPause();
    mEntryProxy.onPause(getActivity());
   
}
  Fragmentの完全コード
public class Fragment_2 extends Fragment{

    EntryProxy mEntryProxy = null;
    FrameLayout rootView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("ceshi","fragment.....1");
        rootView = new FrameLayout(getActivity());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        View rootview=inflater.inflate(R.layout.fragment_2,container,false);

        //   5+        
        WebviewModeListener wm = new WebviewModeListener(getActivity(), rootView);
        //    5+  
        mEntryProxy = EntryProxy.init(getActivity(), wm);
        //   5+  
        mEntryProxy.onCreate(getActivity(), savedInstanceState, SDK.IntegratedMode.WEBVIEW, null);
        Log.i("ceshi","fragment.....2");

        return rootView;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i("ceshi","fragment.....onDestroy");
        mEntryProxy.onStop(getActivity());
    }

    @Override
    public void onResume() {
        super.onResume();
        mEntryProxy.onResume(getActivity());
        Log.i("ceshi","fragment.....onresume");
    }

    @Override
    public void onPause() {
        super.onPause();
        mEntryProxy.onPause(getActivity());
        Log.i("ceshi","fragment.....onpause");
    }
}
class WebviewModeListener implements ICore.ICoreStatusListener {

    LinearLayout btns = null;
    Activity activity = null;
    ViewGroup mRootView = null;
    IWebview webview = null;
    ProgressDialog pd = null;
    public WebviewModeListener(Activity activity, ViewGroup rootView) {
        this.activity = activity;
        mRootView = rootView;
        btns = new LinearLayout(activity);
        mRootView.setBackgroundColor(0xffffffff);
        mRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Log.i("ceshi",webview+"");
                webview.onRootViewGlobalLayout(mRootView);
            }
        });
    }

    /**
     * 5+          
     * */
    @Override
    public void onCoreInitEnd(ICore coreHandler) {
        //         appid
        String appid = "TestAppid";
        //         String url = "file:///android_asset/ceshi.html";
        Log.i("ceshi","fragment.....3");
        webview = SDK.createWebview(activity, url, appid, new IWebviewStateListener() {
            @Override
            public Object onCallBack(int pType, Object pArgs) {
                switch (pType) {
                    case IWebviewStateListener.ON_WEBVIEW_READY: Log.i("ceshi","     +onCallback");
                        //         webview    View ,         ,    webviewhtml        
                        ((IWebview) pArgs).obtainFrameView().obtainMainView().setVisibility(View.INVISIBLE);
                        SDK.attach(mRootView, ((IWebview) pArgs));
                        break;
                    case IWebviewStateListener.ON_PAGE_STARTED: Log.i("ceshi","     +onCallback");
                        //          
                        break;
                    case IWebviewStateListener.ON_PROGRESS_CHANGED: Log.i("ceshi","     +onCallback");
                        //          
                        break;
                    case IWebviewStateListener.ON_PAGE_FINISHED:
                        // webview
                        webview.obtainFrameView().obtainMainView().setVisibility(View.VISIBLE);
                        break;
                }
                return null;
            }
        });

        final WebView webviewInstance = webview.obtainWebview();
        //      
        webviewInstance.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    if (webviewInstance.canGoBack()) {
                        webviewInstance.goBack();
                        return true;
                    }
                }
                return false;
            }
        });
    }

    // 5+SDK         
    @Override
    public void onCoreReady(ICore coreHandler) {
        try {
            //    5+ SDK            // 5+SDK        SDK        
            SDK.initSDK(coreHandler);
            //          5+API
            SDK.requestAllFeature();
            Log.i("ceshi","  ");
            Log.i("ceshi","fragment.....4");
        } catch (Exception e) {
            Log.i("ceshi","  ");
            e.printStackTrace();
        }
        Log.i("ceshi","  ");
    }

    /**
     *     //              
     * private void regNewApi() {
     *       //      js    
     *    String featureName = "T";
     *    //          
     *    String className = "com.HBuilder.integrate.webview.WebViewMode_FeatureImpl";
     *    //      JS      
     *    String content = "(function(plus){function test(){return plus.bridge.execSync('T','test',[arguments]);}plus.T = {test:test};})(window.plus);";
     *    //  5+SDK      
     *    SDK.registerJsApi(featureName, className, content);
     * }
     **/

    @Override
    public boolean onCoreStop() {
        // TODO Auto-generated method stub
        return false;
    }
}
四、次に重要な構成です. 
1.sdkのdataファイルをコピーして、パスはHbuilderSDK\Android d SDK\SDK\assitesです. プロジェクトの中のクラスメイトフォルダの下で
2.プロジェクトのJavaフォルダの下にパッケージ名を作成する:io.dcloud、名前は同じで、HbuilderSDK\Android d-SDK\SDK\src\io\dcloudのRInformation.java
書類をコピーする.
3.明細書ファイルに権限を追加する
android:name="android.permission.INTERNET" />
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
android:name="android.permission.GET_TASKS" />
android:name="android.permission.ACCESS_NETWORK_STATE" />
ここにきました ファイルの統合に成功しました. 展示できます
未完です.