Android隱私協(xié)議提示彈窗的實現(xiàn)流程詳解
android studio版本:2021.2.1
例程名稱:pravicydialog
功能:
1、啟動app后彈窗隱私協(xié)議
2、屏蔽返回鍵
3、再次啟動不再顯示隱私協(xié)議。
本例程的絕大部分代碼來自下面鏈接,因為本人改了一些,增加了一些功能,所以不有臉的算原創(chuàng)了。
下面這個例子是“正宗”app隱私協(xié)議實現(xiàn)方法,而且協(xié)議內(nèi)容使用的是txt格式文件,據(jù)說如果使用html格式文件,各大平臺在審核的時候大概率無法通過,但協(xié)議內(nèi)容的還應(yīng)該有更詳細協(xié)議及說明的鏈接,我沒做,暫時還沒學(xué)會,會了再修改一下。
Android 實現(xiàn)隱私政策提示彈窗
對原作者表示感謝!
直接上代碼:
MainActivity.java
/*
完成日期:2023年1月28日
功能:app協(xié)議頁
1、打開app彈出協(xié)議,禁止返回鍵取消顯示。
2、再次打開協(xié)議頁不再彈出。
*/
package com.example.pravicydialog;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
public class MainActivity extends AppCompatActivity {
Dialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PravicyCheck();
}
public void onClickAgree(View v)
{
dialog.dismiss();
//下面將已閱讀標(biāo)志寫入文件,再次啟動的時候判斷是否顯示。
this.getSharedPreferences("file", Context.MODE_PRIVATE).edit()
.putBoolean("AGREE", true)
.apply();
}
public void onClickDisagree(View v)
{
System.exit(0);//退出軟件
}
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("隱私政策授權(quán)提示");
TextView tv_content = (TextView) inflate.findViewById(R.id.tv_content);
tv_content.setText(str);
dialog = new AlertDialog
.Builder(MainActivity.this)
.setView(inflate)
.show();
// 通過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.setCancelable(false);//屏蔽返回鍵
dialog.getWindow().setAttributes(params);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
}
/**
* 從assets下的txt文件中讀取數(shù)據(jù)
*/
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();
}
public void PravicyCheck(){
Boolean status =this.getSharedPreferences("file",Context.MODE_PRIVATE)
.getBoolean("AGREE",false);
if (status==true){
}else{
showPrivacy("privacy.txt");//放在assets目錄下的隱私政策文本文件
}
}
}說明:
1、dialog.setCancelable(false);屏蔽返回鍵
2、將已閱讀標(biāo)志寫入文件,再次啟動的時候判斷是否顯示。
preferences用法見,實現(xiàn)不同,原理一樣:分享一個SharedPreferences的工具類,方便保存數(shù)據(jù)
this.getSharedPreferences("file", Context.MODE_PRIVATE).edit()
.putBoolean("AGREE", true)
.apply();3、判斷是否是第一次啟動代碼塊:
public void PravicyCheck(){
//讀標(biāo)志
Boolean status =this.getSharedPreferences("file",Context.MODE_PRIVATE)
.getBoolean("AGREE",false);
if (status==true){
//如果status為true,不顯示對話框,直接進主頁面。
}else{
//如果status不為true顯示對話框
showPrivacy("privacy.txt");//放在assets目錄下的隱私政策文本文件
}activity_main.xml(這個是主頁面,可以什么都不放,我放了一個textview)
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="歡迎使用本app?。?
android:textColor="#E91E63"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>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="隱私政策授權(quán)提示"
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="130dp"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginRight="15dp"
android:text="同意"
android:onClick="onClickAgree"
android:textColor="#FF0006"
android:background="@drawable/button_shape"/>
<Button
android:id="@+id/btn_disagree"
android:layout_width="130dp"
android:layout_marginBottom="2dp"
android:layout_height="wrap_content"
android:text="放棄使用"
android:onClick="onClickDisagree"
android:textColor="#000000"
android:background="@drawable/button_shape"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>button_shape.xml(按鈕形狀等屬性)
<?xml version="1.0" encoding="utf-8" ?>
<!--相當(dāng)于做了一張圓角的圖片,然后給button作為背景圖片-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--設(shè)置背景色-->
<solid android:color="#F59E27" />
<!--設(shè)置圓角-->
<corners android:radius="105dip" />
<padding
android:bottom="2dp"
android:left="33dp"
android:right="33dp"
android:top="2dp">
</padding>
</shape>dialog_privacy_shape.xml(對話框?qū)傩裕?/p>
<?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>各個文件位置如圖:

最后動圖:

到此這篇關(guān)于Android隱私協(xié)議提示彈窗的實現(xiàn)流程詳解的文章就介紹到這了,更多相關(guān)Android提示彈窗內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android App將數(shù)據(jù)寫入內(nèi)部存儲和外部存儲的示例
這篇文章主要介紹了Android App將數(shù)據(jù)寫入內(nèi)部存儲和外部存儲的示例,使用外部存儲即訪問并寫入SD卡,需要的朋友可以參考下2016-03-03
Kotlin 使用Lambda來設(shè)置回調(diào)的操作
這篇文章主要介紹了Kotlin 使用Lambda來設(shè)置回調(diào)的操作方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
詳解Android studio實現(xiàn)語音轉(zhuǎn)文字功能
這篇文章主要介紹了如何通過Android studio調(diào)用科大訊飛的語音轉(zhuǎn)文字功能,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2022-03-03
android studio logcat 無篩選 顯示全部日志 無應(yīng)用包名區(qū)分方式
這篇文章主要介紹了android studio logcat 無篩選 顯示全部日志 無應(yīng)用包名區(qū)分方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Android實現(xiàn)仿excel數(shù)據(jù)表格效果
這篇文章主要介紹了Android實現(xiàn)仿excel數(shù)據(jù)表格效果的實現(xiàn)代碼,非常不錯具有參考借鑒價值,需要的朋友可以參考下2016-10-10
android Matrix實現(xiàn)圖片隨意放大縮小或拖動
這篇文章主要為大家詳細介紹了android Matrix實現(xiàn)圖片隨意放大縮小或拖動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02
Android AndBase框架使用封裝好的函數(shù)完成Http請求(三)
這篇文章主要介紹了Android AndBase框架使用封裝好的函數(shù)完成Http請求的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-03-03
Android Studio中CodeStyle模板的配置方式
這篇文章主要介紹了Android Studio中CodeStyle模板的配置方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03

