Android 隐私协议提示弹窗实现

渠道要求在用户同意隐私协议之前,App 不可以调用相关敏感 API,同意后方能进行后续进程,参考网上的资料自己做了个demo【实现启动时检测是否需要弹出提示框,同意进入游戏,不同意退出游戏,第二次进入时,若之前同意过直接进入游戏】。

实现代码如下:

创建加载页MyLaunchActivity,实现检测是否需要弹出隐私提示框以及通过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;
	}
}

 创建弹窗MyPrivacyDialog,实现弹窗内容的展示,点击事件的处理

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);
    }
}


效果图如下: 

Android 隐私协议提示弹窗实现_第1张图片

Android 隐私协议提示弹窗实现_第2张图片 

 

 demo下载:https://github.com/jjinglover/MyCocosDemo

参考资料:https://blog.csdn.net/m0_51282960/article/details/126668101

你可能感兴趣的:(Android,android隐私协议弹窗)