基于egret安卓项目,在进入游戏前加入一个同意隐私权限的界面

基于egret安卓项目,在进入游戏前加入一个同意隐私权限的界面
(1)最终展示:
            如上图
(2)实现:
    在进入egret游戏界面前,先出来一个界面,玩家点了隐私政策的同意,再执行现有的进入egret界面的代码,
等于插入一个界面再执行现有的代码,现有的东西不变
(3)步骤:
    1.在res/drawable文件夹下新建一个按钮样式,新建一个button_shape.xml文件,内容是
    <?xml version="1.0" encoding="utf-8"?>
    <!--相当于做了一张圆角的图片,然后给button作为背景图片-->
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
        <!--设置背景色-->
        <solid android:color="#F59E27" />
        <!--设置圆角-->
        <corners android:radius="105dip" />
        <padding
            android:bottom="2dp"
            android:left="33dp"
            android:right="33dp"
            android:top="2dp">
        </padding>
    </shape>
    2.展示中两个按钮颜色不一样,我又复制上诉按钮xml文件,新建了一个button_shape1.xml文件,只是改了里面背景色的颜色,改成灰色
    <?xml version="1.0" encoding="utf-8"?>
    <!--相当于做了一张圆角的图片,然后给button作为背景图片-->
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
        <!--设置背景色-->
        <solid android:color="#D4D7D8" />
        <!--设置圆角-->
        <corners android:radius="105dip" />
        <padding
            android:bottom="2dp"
            android:left="33dp"
            android:right="33dp"
            android:top="2dp">
        </padding>
    </shape>
    3.在res/drawable文件夹下新建一个弹窗背景的文件,在drawable文件夹下新建dialog_privacy_shape.xml
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
        <!-- 填充色 -->
        <solid android:color="#ffffff" />
        <!-- 矩形圆角半径 -->
        <corners android:radius="10dp" />
    </shape>
    4.在assets文件夹下新建privacy.txt,内容为弹窗主体信息。
    5.在layout文件夹下新建一个布局dialog_privacy_show.xml,这个布局界面里使用到了我们上面建立的按钮和背景样式
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/dialog_privacy_shape"
        android:orientation="vertical">

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_above="@+id/ll_btn_bottom"
                android:layout_marginBottom="15dp"
                android:gravity="center"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tv_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:layout_marginBottom="10dp"
                    android:text="隐私政策"
                    android:textColor="#000000"
                    android:textSize="18sp" />

                <ScrollView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="5dp"
                    android:fadingEdgeLength="50dp"
                    android:requiresFadingEdge="horizontal">

                    <TextView
                        android:id="@+id/tv_content"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginTop="10dp"
                        android:singleLine="false"
                        android:text=""
                        android:textColor="#000000" />
                </ScrollView>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/ll_btn_bottom"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:gravity="center"

                >

                <Button
                    android:id="@+id/btn_agree"
                    android:layout_width="100dp"
                    android:layout_height="35dp"
                    android:layout_marginRight="15dp"
                    android:layout_marginBottom="2dp"
                    android:background="@drawable/button_shape"
                    android:onClick="onClickAgree"
                    android:text="同意"
                    android:textColor="#000000"
                    android:textStyle="bold" />

                <Button
                    android:id="@+id/btn_disagree"
                    android:layout_width="100dp"
                    android:layout_height="35dp"
                    android:layout_marginBottom="2dp"
                    android:background="@drawable/button_shape1"
                    android:onClick="onClickDisagree"
                    android:text="拒绝"
                    android:textColor="#000000" />

            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>
    6.界面的xml文件准备好了,至于想调节页面样式,你自己调就好了,按钮上的字啊,大小啊
    7.去代码层,把egret启动自己的layout界面先提取出来
    private void enterApp(){
        //这段代码就是egret启动自己layout界面的代码,调了它就会进入我们的egret游戏了
        if (!nativeAndroid.initialize(gameUrl)) {
            Toast.makeText(this, "Initialize native failed.",
                    Toast.LENGTH_LONG).show();
            return;
        }
        rootLayout = nativeAndroid.getRootFrameLayout();
        setContentView(rootLayout);
        showLoadingView();
        //这里可以把你接的第三方的sdk的初始化放在这里,或者请求权限什么的都放在这
        //隐私政策要求在请求权限前,必须弹出隐私政策的弹框,所以得等玩家点了隐私政策的同意后才能请求权限
    }
    8.隐私政策规定,要在首次进入app的时候弹出来,不是首次就不用了,这李我们的实现方式是缓存
    这就是我们现在进入游戏的代码,SharedPreferences缓存中没东西,则弹出隐私政策,否则我们直接进入游戏
    //下面代码复制到oncreate函数中
    String str = "xuZeLin";     //SharedPreferences没有设置版本键值对之前的初始字符串
    String version = getVersion();
    if(version.equals(str)){
        //隐私政策弹框
        showPrivacy("privacy.txt");//放在assets目录下的隐私政策文本文件
    }else {
        enterApp();
    }
    9.显示隐私政策弹窗的函数
    public void showPrivacy(String privacyFileName)
    {
        String str = initAssets(privacyFileName);
        final View inflate = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_privacy_show, null);
//        TextView tv_title = (TextView) inflate.findViewById(R.id.tv_title);
//        tv_title.setText("隐私政策");
        TextView tv_content = (TextView) inflate.findViewById(R.id.tv_content);
        tv_content.setText(str.replace("|","\n"));
        dialog = new MyDialog
                .Builder(MainActivity.this)
                .show();
        //必须使用这个方法,不能使用dialog.setView()的方法,为了弹窗外面不能有一个边框
        dialog.setContentView(inflate);
        //设置Dialog点击背景屏幕 Dialog不消失,不写这个,点击弹框的背景,弹窗会被关闭
        dialog.setCanceledOnTouchOutside(false);
        // 通过WindowManager获取
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        final WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = dm.widthPixels*4/5;
        params.height = dm.heightPixels*1/2;
        dialog.getWindow().setAttributes(params);
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    }
    /**
     * 从assets下的txt文件中读取数据
     */
    public String initAssets(String fileName) {
        String str = null;
        try {
            InputStream inputStream = getAssets().open(fileName);

            str = getString(inputStream);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return str;
    }
    public static String getString(InputStream inputStream) {
        InputStreamReader inputStreamReader = null;
        try {
            inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        BufferedReader reader = new BufferedReader(inputStreamReader);
        StringBuffer sb = new StringBuffer("");
        String line;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line);
                sb.append("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }
    10.隐私政策界面上两个按钮的事件绑定,吧下面函数复制到mainActivity中
    //点击同意隐私政策
    public void onClickAgree(View v)
    {
        dialog.dismiss();
//        ((ViewGroup)view.getParent()).removeView(view);
        setVersion(xcInitVersion);
        enterApp();
    }
    //点击拒绝隐私政策
    public void onClickDisagree(View v)
    {
        //关掉app
        finish();
    }
    11.上面有个MyDialog类没有,这个是自己建的类,不用这个,做出来的dialog外面会带一个边框
    12.在values下新建一个mydialogstyle.xml文件
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <style name="ShareDialog"  parent="android:Theme.Dialog">
            <item name="android:windowFrame">@null</item>
            <item name="android:windowNoTitle">true</item>
            <item name="android:background">@android:color/black</item>
            <item name="android:windowBackground">@android:color/black</item>
            <item name="android:windowIsFloating">true</item>
            <item name="android:windowContentOverlay">@null</item>
        </style>
    </resources>
    13.在mainActivity类同级下新建一个MyDialog类,继承dialog
    package org.egret.launcher.canlongbabytaptest;

    import android.app.AlertDialog;
    import android.content.Context;

    public class MyDialog extends AlertDialog {
        public MyDialog(Context context) {
            super(context,R.style.ShareDialog);
        }
    }
    14.这杨dialog就没外框了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值