WebApp方式集成5+ sdk

本文介绍了一种使用5+SDK集成WebView界面的方法,并提供了一个具体的实现案例。该方案适用于需要自定义标题栏的WebApp应用,通过示例代码展示了如何在Android应用中集成并使用5+SDK来实现WebView功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

WebApp方式集成5+ sdk的方式集成webview界面的时候,一般都需要我们自己添加一个原生的title。

直接上代码

 public class SDK_WebApp extends Activity {

    boolean doHardAcc = true;
    EntryProxy mEntryProxy = null;
    private ArrayList<String> mUrls;
    private boolean isBack = true;
    private ImageView sharedBtn;
    private String currentUrl;
    // private CustomShareBoard shareBoard;
    private Button leftBtn;
    private Button mBtnBack;
    private WebView rootView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.me_agency_web);
        // 4.4及以上版本开启
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // setTranslucentStatus(true);
        }
        // SystemBarTintManager tintManager = new SystemBarTintManager(this);
        // tintManager.setStatusBarTintEnabled(true);
        // tintManager.setNavigationBarTintEnabled(true);
        // 自定义颜色
        // tintManager.setTintColor(Color.parseColor("#FF9000"));

        sharedBtn = (ImageView) findViewById(R.id.sharedBtn);
        mBtnBack = (Button) findViewById(R.id.back_Btn);
        leftBtn = (Button) findViewById(R.id.leftBtn);
        // leftBtn.setOnClickListener(this);
        // mBtnBack.setOnClickListener(this);
        // sharedBtn.setOnClickListener(this);

        if (mEntryProxy == null) {
            rootView = (WebView) findViewById(R.id.web_include_webview);
            // shareBoard = new CustomShareBoard(MeAgencyWebActivity.this);
            mUrls = new ArrayList<String>();

            // 创建5+内核运行事件监听
            WebviewModeListener wm = new WebviewModeListener(this, rootView);
            // 初始化5+内核
            mEntryProxy = EntryProxy.init(this, wm);
            // 启动5+内核
            mEntryProxy.onCreate(this, savedInstanceState,
                    SDK.IntegratedMode.WEBVIEW, null);
            // setContentView(rootView);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return mEntryProxy.onActivityExecute(this,
                SysEventType.onCreateOptionMenu, menu);
    }

    @Override
    public void onPause() {
        super.onPause();
        mEntryProxy.onPause(this);
    }

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

    public void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent.getFlags() != 0x10600000) {// 非点击icon调用activity时才调用newintent事件
            mEntryProxy.onNewIntent(this, intent);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mEntryProxy.onStop(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        boolean _ret = mEntryProxy.onActivityExecute(this,
                SysEventType.onKeyDown, new Object[] { keyCode, event });
        return _ret ? _ret : super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        boolean _ret = mEntryProxy.onActivityExecute(this,
                SysEventType.onKeyUp, new Object[] { keyCode, event });
        return _ret ? _ret : super.onKeyUp(keyCode, event);
    }

    @Override
    public boolean onKeyLongPress(int keyCode, KeyEvent event) {
        boolean _ret = mEntryProxy.onActivityExecute(this,
                SysEventType.onKeyLongPress, new Object[] { keyCode, event });
        return _ret ? _ret : super.onKeyLongPress(keyCode, event);
    }

    public void onConfigurationChanged(Configuration newConfig) {
        try {
            int temp = this.getResources().getConfiguration().orientation;
            if (mEntryProxy != null) {
                mEntryProxy.onConfigurationChanged(this, temp);
            }
            super.onConfigurationChanged(newConfig);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        mEntryProxy.onActivityExecute(this, SysEventType.onActivityResult,
                new Object[] { requestCode, resultCode, data });
    }
}

class WebappModeListener implements ICoreStatusListener, IOnCreateSplashView {
    Activity activity;
    View splashView = null;
    ViewGroup rootView;
    IApp app = null;
    ProgressDialog pd = null;

    public WebappModeListener(Activity activity, ViewGroup rootView) {
        this.activity = activity;
        this.rootView = rootView;
    }

    /**
     * 5+内核初始化完成时触发
     * */
    @Override
    public void onCoreInitEnd(ICore coreHandler) {

        // 表示Webapp的路径在 file:///android_asset/apps/HelloH5
        String appBasePath = "/apps/HelloH5";

        // 设置启动参数,可在页面中通过plus.runtime.arguments;方法获取到传入的参数
        String args = "{url:'http://www.baidu.com'}";

        // 启动启动独立应用的5+ Webapp
        app = SDK.startWebApp(activity, appBasePath, args,
                new IWebviewStateListener() {
                    // 设置Webview事件监听,可在监监听内获取WebIvew加载内容的进度
                    @Override
                    public Object onCallBack(int pType, Object pArgs) {
                        switch (pType) {
                        case IWebviewStateListener.ON_WEBVIEW_READY:
                            // WebApp准备加载事件
                            // 准备完毕之后添加webview到显示父View中,
                            // 设置排版不显示状态,避免显示webview时html内容排版错乱问题
                            View view = ((IWebview) pArgs).obtainApp()
                                    .obtainWebAppRootView().obtainMainView();
                            view.setVisibility(View.INVISIBLE);

                            if (view.getParent() != null) {
                                ((ViewGroup) view.getParent()).removeView(view);
                            }
                            rootView.addView(view, 0);
                            break;
                        case IWebviewStateListener.ON_PAGE_STARTED:
                            // 首页面开始加载事件
                            pd = ProgressDialog.show(activity, "加载中", "0/100");
                            break;
                        case IWebviewStateListener.ON_PROGRESS_CHANGED:
                            // WebApp首页面加载进度变化事件
                            if (pd != null) {
                                pd.setMessage(pArgs + "/100");
                            }
                            break;
                        case IWebviewStateListener.ON_PAGE_FINISHED:
                            // WebApp首页面加载完成事件
                            if (pd != null) {
                                pd.dismiss();
                                pd = null;
                            }
                            // 页面加载完毕,设置显示webview
                            // 如果不需要显示spalsh页面将此行代码移动至onCloseSplash事件内
                            app.obtainWebAppRootView().obtainMainView()
                                    .setVisibility(View.VISIBLE);
                            break;
                        }
                        return null;
                    }
                }, this);

        app.setIAppStatusListener(new IAppStatusListener() {
            // 设置APP运行事件监听
            @Override
            public boolean onStop() {
                // 应用运行停止时调用
                rootView.removeView(app.obtainWebAppRootView().obtainMainView());
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public void onStart() {
                // 独立应用启动时触发事件
            }

            @Override
            public void onPause(IApp arg0, IApp arg1) {
                // WebApp暂停运行时触发事件
            }

            @Override
            public String onStoped(boolean arg0, String arg1) {
                // TODO Auto-generated method stub
                return null;
            }
        });
    }

    @Override
    public void onCoreReady(ICore coreHandler) {
        // 初始化5+ SDK,
        // 5+SDK的其他接口需要在SDK初始化后才能調用
        SDK.initSDK(coreHandler);
        // 设置当前应用可使用的5+ API
        SDK.requestAllFeature();
    }

    @Override
    public boolean onCoreStop() {
        // 当返回false时候回关闭activity
        return false;
    }

    // 在Widget集成时如果不需要显示splash页面可按照如下步骤操作
    // 1 删除onCreateSplash方法内的代码
    // 2 将5+mainView添加到rootview时将页面设置为不可见
    // 3 在onCloseSplash方法中将5+mainView设置为可见
    // 4 修改androidmanifest.xml文件 将SDK_WebApp的主题设置为透明
    // 注意!
    // 如果不显示splash页面会造成用户点击后页面短时间内会没有变化,
    // 可能会给用户造成程序没响应的错觉,
    // 所以开发者需要对5+内核启动到5+应用页面显示之间的这段事件进行处理

    @Override
    public Object onCreateSplash(Context pContextWrapper) {
        splashView = new FrameLayout(activity);
        splashView.setBackgroundResource(RInformation.DRAWABLE_SPLASH);
        rootView.addView(splashView);
        return null;
    }

    @Override
    public void onCloseSplash() {
        rootView.removeView(splashView);
    }
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="true"
    android:fitsSystemWindows="true"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/back_Btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="5dp"
            android:paddingTop="5dp" />

        <Button
            android:id="@+id/leftBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="50dp"
            android:padding="10dp"
            android:text="关闭"
            android:visibility="gone" />

        <TextView
            android:id="@+id/titleTv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:padding="10dp"
            android:text="客户经理"
            android:textColor="#ffffff" />

        <TextView
            android:id="@+id/sharedBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:paddingBottom="10dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:paddingTop="10dp"
            android:text="分享"
            android:visibility="gone" />
    </RelativeLayout>

    <ProgressBar
        android:id="@+id/progress_main"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:visibility="gone" />

    <WebView
        android:id="@+id/web_include_webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值