Android隱私協(xié)議提示彈窗的實(shí)現(xiàn)流程詳解
android studio版本:2021.2.1
例程名稱:pravicydialog
功能:
1、啟動(dòng)app后彈窗隱私協(xié)議
2、屏蔽返回鍵
3、再次啟動(dòng)不再顯示隱私協(xié)議。
本例程的絕大部分代碼來自下面鏈接,因?yàn)楸救烁牧艘恍?,增加了一些功能,所以不有臉的算原?chuàng)了。
下面這個(gè)例子是“正宗”app隱私協(xié)議實(shí)現(xiàn)方法,而且協(xié)議內(nèi)容使用的是txt格式文件,據(jù)說如果使用html格式文件,各大平臺(tái)在審核的時(shí)候大概率無法通過,但協(xié)議內(nèi)容的還應(yīng)該有更詳細(xì)協(xié)議及說明的鏈接,我沒做,暫時(shí)還沒學(xué)會(huì),會(huì)了再修改一下。
Android 實(shí)現(xiàn)隱私政策提示彈窗
對(duì)原作者表示感謝!
直接上代碼:
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)志寫入文件,再次啟動(dòng)的時(shí)候判斷是否顯示。
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)志寫入文件,再次啟動(dòng)的時(shí)候判斷是否顯示。
preferences用法見,實(shí)現(xiàn)不同,原理一樣:分享一個(gè)SharedPreferences的工具類,方便保存數(shù)據(jù)
this.getSharedPreferences("file", Context.MODE_PRIVATE).edit()
.putBoolean("AGREE", true)
.apply();3、判斷是否是第一次啟動(dòng)代碼塊:
public void PravicyCheck(){
//讀標(biāo)志
Boolean status =this.getSharedPreferences("file",Context.MODE_PRIVATE)
.getBoolean("AGREE",false);
if (status==true){
//如果status為true,不顯示對(duì)話框,直接進(jìn)主頁面。
}else{
//如果status不為true顯示對(duì)話框
showPrivacy("privacy.txt");//放在assets目錄下的隱私政策文本文件
}activity_main.xml(這個(gè)是主頁面,可以什么都不放,我放了一個(gè)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(對(duì)話框)
<?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(對(duì)話框?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>各個(gè)文件位置如圖:

最后動(dòng)圖:

到此這篇關(guān)于Android隱私協(xié)議提示彈窗的實(shí)現(xiàn)流程詳解的文章就介紹到這了,更多相關(guān)Android提示彈窗內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android?Jetpack組件ViewModel基本用法詳解
- Android進(jìn)階從字節(jié)碼插樁技術(shù)了解美團(tuán)熱修復(fù)實(shí)例詳解
- Android進(jìn)階Handler應(yīng)用線上卡頓監(jiān)控詳解
- Android進(jìn)階KOOM線上APM監(jiān)控全面剖析
- Android進(jìn)階CoordinatorLayout協(xié)調(diào)者布局實(shí)現(xiàn)吸頂效果
- Android進(jìn)階NestedScroll嵌套滑動(dòng)機(jī)制實(shí)現(xiàn)吸頂效果詳解
- Android進(jìn)階之從IO到NIO的模型機(jī)制演進(jìn)
相關(guān)文章
Android編程中TextView寬度過大導(dǎo)致Drawable無法居中問題解決方法
這篇文章主要介紹了Android編程中TextView寬度過大導(dǎo)致Drawable無法居中問題解決方法,以實(shí)例形式較為詳細(xì)的分析了TextView設(shè)置及xml布局與調(diào)用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android 無障礙全局懸浮窗實(shí)現(xiàn)示例
本文主要介紹了Android 無障礙全局懸浮窗實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
Android實(shí)現(xiàn)音樂播放進(jìn)度條傳遞信息的兩種方式(在service和activity中)
這篇文章主要介紹了Android:在service和activity之中,實(shí)現(xiàn)音樂播放進(jìn)度條傳遞信息的兩種方式,MediaPlayer做音樂播放器采坑以及解決辦法,需要的朋友可以參考下2020-05-05
Android Studio 在項(xiàng)目中引用第三方j(luò)ar包的方法
本文分步驟給大家介紹了Android Studio 在項(xiàng)目中引用第三方j(luò)ar包的方法,感興趣的朋友跟隨小編一起看看吧2018-09-09
Android實(shí)現(xiàn)簡單實(shí)用的搜索框
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡單實(shí)用的搜索框,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10

