经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android隐私协议提示弹窗的实现流程详解
来源:jb51  时间:2023/2/1 9:35:49  对本文有异议

android studio版本:2021.2.1

例程名称:pravicydialog

功能:

1、启动app后弹窗隐私协议

2、屏蔽返回键

3、再次启动不再显示隐私协议。

本例程的绝大部分代码来自下面链接,因为本人改了一些,增加了一些功能,所以不有脸的算原创了。

下面这个例子是“正宗”app隐私协议实现方法,而且协议内容使用的是txt格式文件,据说如果使用html格式文件,各大平台在审核的时候大概率无法通过,但协议内容的还应该有更详细协议及说明的链接,我没做,暂时还没学会,会了再修改一下。

Android 实现隐私政策提示弹窗

对原作者表示感谢!

直接上代码:

MainActivity.java

  1. /*
  2. 完成日期:2023年1月28日
  3. 功能:app协议页
  4. 1、打开app弹出协议,禁止返回键取消显示。
  5. 2、再次打开协议页不再弹出。
  6. */
  7. package com.example.pravicydialog;
  8. import androidx.appcompat.app.AppCompatActivity;
  9. import android.app.AlertDialog;
  10. import android.app.Dialog;
  11. import android.content.Context;
  12. import android.os.Bundle;
  13. import android.util.DisplayMetrics;
  14. import android.view.LayoutInflater;
  15. import android.view.View;
  16. import android.view.WindowManager;
  17. import android.widget.TextView;
  18. import java.io.BufferedReader;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.InputStreamReader;
  22. import java.io.UnsupportedEncodingException;
  23. public class MainActivity extends AppCompatActivity {
  24. Dialog dialog;
  25. @Override
  26. protected void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.activity_main);
  29. PravicyCheck();
  30. }
  31. public void onClickAgree(View v)
  32. {
  33. dialog.dismiss();
  34. //下面将已阅读标志写入文件,再次启动的时候判断是否显示。
  35. this.getSharedPreferences("file", Context.MODE_PRIVATE).edit()
  36. .putBoolean("AGREE", true)
  37. .apply();
  38. }
  39. public void onClickDisagree(View v)
  40. {
  41. System.exit(0);//退出软件
  42. }
  43. public void showPrivacy(String privacyFileName){
  44. String str = initAssets(privacyFileName);
  45. final View inflate = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_privacy_show, null);
  46. TextView tv_title = (TextView) inflate.findViewById(R.id.tv_title);
  47. tv_title.setText("隐私政策授权提示");
  48. TextView tv_content = (TextView) inflate.findViewById(R.id.tv_content);
  49. tv_content.setText(str);
  50. dialog = new AlertDialog
  51. .Builder(MainActivity.this)
  52. .setView(inflate)
  53. .show();
  54. // 通过WindowManager获取
  55. DisplayMetrics dm = new DisplayMetrics();
  56. getWindowManager().getDefaultDisplay().getMetrics(dm);
  57. final WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
  58. params.width = dm.widthPixels*4/5;
  59. params.height = dm.heightPixels*1/2;
  60. dialog.setCancelable(false);//屏蔽返回键
  61. dialog.getWindow().setAttributes(params);
  62. dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
  63. }
  64. /**
  65. * 从assets下的txt文件中读取数据
  66. */
  67. public String initAssets(String fileName) {
  68. String str = null;
  69. try {
  70. InputStream inputStream = getAssets().open(fileName);
  71. str = getString(inputStream);
  72. } catch (IOException e1) {
  73. e1.printStackTrace();
  74. }
  75. return str;
  76. }
  77. public static String getString(InputStream inputStream) {
  78. InputStreamReader inputStreamReader = null;
  79. try {
  80. inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
  81. } catch (UnsupportedEncodingException e1) {
  82. e1.printStackTrace();
  83. }
  84. BufferedReader reader = new BufferedReader(inputStreamReader);
  85. StringBuffer sb = new StringBuffer("");
  86. String line;
  87. try {
  88. while ((line = reader.readLine()) != null) {
  89. sb.append(line);
  90. sb.append("\n");
  91. }
  92. } catch (IOException e) {
  93. e.printStackTrace();
  94. }
  95. return sb.toString();
  96. }
  97. public void PravicyCheck(){
  98. Boolean status =this.getSharedPreferences("file",Context.MODE_PRIVATE)
  99. .getBoolean("AGREE",false);
  100. if (status==true){
  101. }else{
  102. showPrivacy("privacy.txt");//放在assets目录下的隐私政策文本文件
  103. }
  104. }
  105. }

说明:

1、dialog.setCancelable(false);屏蔽返回键

2、将已阅读标志写入文件,再次启动的时候判断是否显示。

preferences用法见,实现不同,原理一样:分享一个SharedPreferences的工具类,方便保存数据

  1. this.getSharedPreferences("file", Context.MODE_PRIVATE).edit()
  2. .putBoolean("AGREE", true)
  3. .apply();

3、判断是否是第一次启动代码块:

  1. public void PravicyCheck(){
  2. //读标志
  3. Boolean status =this.getSharedPreferences("file",Context.MODE_PRIVATE)
  4. .getBoolean("AGREE",false);
  5. if (status==true){
  6. //如果status为true,不显示对话框,直接进主页面。
  7. }else{
  8. //如果status不为true显示对话框
  9. showPrivacy("privacy.txt");//放在assets目录下的隐私政策文本文件
  10. }

activity_main.xml(这个是主页面,可以什么都不放,我放了一个textview)

  1. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:app="http://schemas.android.com/apk/res-auto"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="horizontal">
  6. <TextView
  7. android:id="@+id/textView2"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="欢迎使用本app!!"
  11. android:textColor="#E91E63"
  12. android:textSize="30sp"
  13. app:layout_constraintBottom_toBottomOf="parent"
  14. app:layout_constraintEnd_toEndOf="parent"
  15. app:layout_constraintStart_toStartOf="parent"
  16. app:layout_constraintTop_toTopOf="parent" />
  17. </androidx.constraintlayout.widget.ConstraintLayout>

dialog_privacy_show.xml(对话框)

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:background="@drawable/dialog_privacy_shape"
  7. android:orientation="vertical">
  8. <RelativeLayout
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent">
  11. <LinearLayout
  12. android:layout_width="match_parent"
  13. android:layout_height="match_parent"
  14. android:layout_above="@+id/ll_btn_bottom"
  15. android:layout_marginBottom="15dp"
  16. android:gravity="center"
  17. android:orientation="vertical">
  18. <TextView
  19. android:id="@+id/tv_title"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:layout_marginTop="10dp"
  23. android:layout_marginBottom="10dp"
  24. android:text="隐私政策授权提示"
  25. android:textColor="#000000"
  26. android:textSize="18sp" />
  27. <ScrollView
  28. android:layout_width="match_parent"
  29. android:layout_height="match_parent"
  30. android:layout_marginLeft="5dp"
  31. android:layout_marginRight="5dp"
  32. android:fadingEdgeLength="50dp"
  33. android:requiresFadingEdge="horizontal">
  34. <TextView
  35. android:id="@+id/tv_content"
  36. android:layout_width="match_parent"
  37. android:layout_height="match_parent"
  38. android:layout_marginTop="10dp"
  39. android:singleLine="false"
  40. android:text=""
  41. android:textColor="#000000" />
  42. </ScrollView>
  43. </LinearLayout>
  44. <LinearLayout
  45. android:id="@+id/ll_btn_bottom"
  46. android:layout_width="match_parent"
  47. android:layout_height="wrap_content"
  48. android:layout_alignParentBottom="true"
  49. android:gravity="center"
  50. >
  51. <Button
  52. android:id="@+id/btn_agree"
  53. android:layout_width="130dp"
  54. android:layout_height="wrap_content"
  55. android:layout_marginBottom="2dp"
  56. android:layout_marginRight="15dp"
  57. android:text="同意"
  58. android:onClick="onClickAgree"
  59. android:textColor="#FF0006"
  60. android:background="@drawable/button_shape"/>
  61. <Button
  62. android:id="@+id/btn_disagree"
  63. android:layout_width="130dp"
  64. android:layout_marginBottom="2dp"
  65. android:layout_height="wrap_content"
  66. android:text="放弃使用"
  67. android:onClick="onClickDisagree"
  68. android:textColor="#000000"
  69. android:background="@drawable/button_shape"/>
  70. </LinearLayout>
  71. </RelativeLayout>
  72. </LinearLayout>

button_shape.xml(按钮形状等属性)

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!--相当于做了一张圆角的图片,然后给button作为背景图片-->
  3. <shape xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:shape="rectangle">
  5. <!--设置背景色-->
  6. <solid android:color="#F59E27" />
  7. <!--设置圆角-->
  8. <corners android:radius="105dip" />
  9. <padding
  10. android:bottom="2dp"
  11. android:left="33dp"
  12. android:right="33dp"
  13. android:top="2dp">
  14. </padding>
  15. </shape>

dialog_privacy_shape.xml(对话框属性)

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:shape="rectangle">
  4. <!-- 填充色 -->
  5. <solid android:color="#ffffff" />
  6. <!-- 矩形圆角半径 -->
  7. <corners android:radius="10dp" />
  8. </shape>

各个文件位置如图:

最后动图:

到此这篇关于Android隐私协议提示弹窗的实现流程详解的文章就介绍到这了,更多相关Android提示弹窗内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号