Realization of Android privacy protocol prompt pop-up window

The channel requires that before the user agrees to the privacy agreement, the app cannot call the relevant sensitive API, and the follow-up process can only be carried out after the user agrees. I made a demo by referring to the information on the Internet [check whether a prompt box needs to be popped up at startup, agree to enter the game, and disagree Exit the game, and enter the game for the second time, if you have agreed before, you can directly enter the game].

The implementation code is as follows:

Create a loading page MyLaunchActivity to detect whether a privacy prompt box needs to pop up and view the privacy agreement through WebView

package org.cocos2dx.cpp.launch;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.sant.test.R;

import org.cocos2dx.cpp.AppActivity;

public class MyLaunchActivity extends Activity {

	private static String _agreedSymbol = "AGREE_HQ_SERVICE";
	private MyPrivacyDialog _agrementDialog=null;
	private FrameLayout _bgLayout=null;
	private TextView _titleTxt=null;
	private WebView _webView=null;
	private Button _exitBtn=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_launch);
		initShow();
		initDialog();
	}

	private void initShow(){
		_bgLayout=findViewById(R.id.launch_con_node);
		_titleTxt=findViewById(R.id.launch_title);
		_exitBtn=findViewById(R.id.launch_exitBtn);
		_exitBtn.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v){
				if(_agrementDialog!=null){
					_agrementDialog.show();
				}
				_bgLayout.setVisibility(View.GONE);
			}
		});

		_webView=findViewById(R.id.launch_WebView);
		WebSettings settings = _webView.getSettings();
		settings.setJavaScriptEnabled(true);
		settings.setDefaultTextEncodingName("UTF-8");
		//自适应手机屏幕
		settings.setUseWideViewPort(true);
		settings.setLoadWithOverviewMode(true);

		_bgLayout.setVisibility(View.GONE);
	}

	private void initDialog(){
		boolean states= (boolean) SPUtil.get(getApplication(),_agreedSymbol,false);
		if (states==false)
		{
			_agrementDialog = new MyPrivacyDialog(this);
			_agrementDialog.show();
			_agrementDialog.setOnBtnClickListener(new MyPrivacyDialog.OnBtnClickListener(){
				@Override
				public void onClick(int type) {
					switch (type) {
						case MyPrivacyDialog.ARGEEMENT_TEXT_CLICK://查看用户协议
							loadUrl(getResources().getString(R.string.url_service),getResources().getString(R.string.user_service));
							break;
						case MyPrivacyDialog.SECRET_TEXT_CLICK://查看隐私协议
							_agrementDialog.hide();
							loadUrl(getResources().getString(R.string.url_private),getResources().getString(R.string.user_private));
							break;
						case MyPrivacyDialog.NOT_ARGEE_BTN_CLICK://不同意按钮
							_agrementDialog.dismiss();
							finish();
							break;
						case MyPrivacyDialog.ARGEE_BTN_CLICK: //同意按钮
							_agrementDialog.dismiss();
							SPUtil.put(getApplication(),_agreedSymbol,true);
							jumpGameActivity();
							break;
					}
				}
			});
		}
		else{
			jumpGameActivity();
		}
	}

	private void loadUrl(String url,String str){
		_bgLayout.setVisibility(View.VISIBLE);
		_titleTxt.setText(str);
		_webView.loadUrl(url);

		_agrementDialog.hide();
	}

	public void jumpGameActivity() {
		// 闪屏结束后,跳转到游戏界面
		Intent intent = new Intent(this, AppActivity.class);
		startActivity(intent);
		this.finish();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{

		}
		return true;
	}
}

 Create a pop-up window MyPrivacyDialog to display the content of the pop-up window and process the click event

package org.cocos2dx.cpp.launch;

import android.os.Bundle;
import android.app.AlertDialog;
import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.sant.test.R;

import androidx.annotation.NonNull;


public class MyPrivacyDialog extends AlertDialog {
    private Context context;
    private TextView agreeTv;
    public static final int ARGEEMENT_TEXT_CLICK = 1; //用户协议
    public static final int SECRET_TEXT_CLICK = 2; //隐私协议
    public static final int ARGEE_BTN_CLICK = 3; //同意按钮
    public static final int NOT_ARGEE_BTN_CLICK = 4; //不同意按钮
    private static int START_AG = 0;  //用户协议需要加颜色的开始文字位置
    private static int END_AG = 0;//结束

    private static int START_SECRET = 0;//隐私开始文字位置
    private static int END_SECRET = 0;//结束
    private OnBtnClickListener listener;
    public MyPrivacyDialog(Context context) {
        super(context);
        this.context = context;
    }

    public interface OnBtnClickListener {
        void onClick(int type);
    }

    public void setOnBtnClickListener(OnBtnClickListener onBtnClickListener) {
        this.listener = onBtnClickListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_dialog_privacy);
        initView();
    }

    private void initView() {
        agreeTv = findViewById(R.id.userArgTv);
        String argContent = context.getResources().getString(R.string.user_content);
        String serviceStr = context.getResources().getString(R.string.user_service);
        String privateStr = context.getResources().getString(R.string.user_private);
        START_AG = argContent.indexOf(serviceStr);
        END_AG = START_AG + serviceStr.length();
        START_SECRET = argContent.indexOf(privateStr);
        END_SECRET = START_SECRET + privateStr.length();

        SpannableString spannableString = new SpannableString(argContent);
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(context.getResources().getColor(R.color.click_txt_color));
        ForegroundColorSpan colorSpan2 = new ForegroundColorSpan(context.getResources().getColor(R.color.click_txt_color));
        UnderlineSpan underlineSpan = new UnderlineSpan();
        UnderlineSpan underlineSpan2 = new UnderlineSpan();
        ClickableSpan userArgeeClick = new ClickableSpan() {
            //用户协议
            @Override
            public void onClick(@NonNull View view) {
                //Toast.makeText(context, "用户协议", Toast.LENGTH_SHORT).show();
                listener.onClick(ARGEEMENT_TEXT_CLICK);
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setUnderlineText(false);//去除连接下划线
                ds.setColor(context.getResources().getColor(R.color.click_txt_color));
                ds.clearShadowLayer();
            }
        };

        ClickableSpan secretClick = new ClickableSpan() {
            //隐私政策
            @Override
            public void onClick(@NonNull View view) {
                //Toast.makeText(context, "隐私政策", Toast.LENGTH_SHORT).show();
                listener.onClick(SECRET_TEXT_CLICK);
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(context.getResources().getColor(R.color.click_txt_color));
                ds.setUnderlineText(false);//去除连接下划线
                ds.clearShadowLayer();

            }
        };


        findViewById(R.id.agreeBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(ARGEE_BTN_CLICK);
            }
        });

        findViewById(R.id.notAgreeBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(NOT_ARGEE_BTN_CLICK);
            }
        });
        spannableString.setSpan(colorSpan, START_AG, END_AG, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        spannableString.setSpan(underlineSpan, START_AG + 1, END_AG - 1, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        spannableString.setSpan(userArgeeClick, START_AG, END_AG, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

        spannableString.setSpan(colorSpan2, START_SECRET, END_SECRET, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        spannableString.setSpan(underlineSpan2, START_SECRET + 1, END_SECRET - 1, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        spannableString.setSpan(secretClick, START_SECRET, END_SECRET, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        agreeTv.setMovementMethod(LinkMovementMethod.getInstance());
        //设置点击背景色透明  解决点击时有阴影效果
        agreeTv.setHighlightColor(context.getResources().getColor(android.R.color.transparent));
        agreeTv.setText(spannableString);
        setCanceledOnTouchOutside(false);
        setCancelable(false);
    }
}


The renderings are as follows: 

 

 

 Demo download: https://github.com/jjinglover/MyCocosDemo

References: https://blog.csdn.net/m0_51282960/article/details/126668101

Guess you like

Origin blog.csdn.net/auccy/article/details/129972886