gpt4 book ai didi

android - 在 android 应用程序中显示许可协议(protocol)

转载 作者:太空狗 更新时间:2023-10-29 15:07:13 25 4
gpt4 key购买 nike

当用户第一次使用该应用程序时,我们必须显示一个许可协议(protocol)对话框,现在我有两个问题:

1 把这个对话框放在哪里?

添加另一个 Activity 或将对话框放在启动 Activity 的 MainActivity 处?

2 如何在用户点击“拒绝”时关闭应用

一旦用户点击“拒绝”按钮,这意味着他/她不同意我们的许可,那么我们必须完全退出应用程序。如何制作?


根据“Ahmad”的回答,我将在 Activity 开始时决定是否打开对话框(onCreate 方法):

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
this.setupLicense();
this.setupViews();
this.initSomeJob();
}

private void setupLicense() {
SharedPreferences setting = getSharedPreferences(IConstant.Map_License, 0);
boolean mapLicenseAccept = setting.getBoolean(IConstant.Map_License, false);
if (!mapLicenseAccept) {
//user does not accept the license yet, we will open the dialog
showDialog(Dialog_Map_License);
}
}
@Override
protected Dialog onCreateDialog(int id) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
switch (id) {
case Dialog_Map_License:
builder.setIconAttribute(android.R.attr.alertDialogIcon)
.setTitle(R.string.map_license_title)
.setMessage(R.string.map_license_content)
.setPositiveButton(R.string.map_license_accept, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//License accepted, persisted it.
SharedPreferences settings = getSharedPreferences(IConstant.Map_License, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(IConstant.Map_License, true);
editor.commit();
}
})
.setNegativeButton(R.string.map_license_reject, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//do nothing and exit
Process.killProcess(Process.myPid());
System.exit(0);
}
});
Dialog target = builder.create();
target.setCanceledOnTouchOutside(false);
return target;
}
return null;
}

但是现在我遇到了两个问题:

1 事件我选择“接受”按钮,当我第二次打开我的应用程序时,对话框将显示。

看来下面的代码不起作用:

                            editor.putBoolean(IConstant.Map_License, true);
editor.commit();

2 当我显示对话框时,代码:

this.setupViews();
this.initSomeJob();

仍会运行,它们不会被阻止,这出乎意料,因为在用户点击“接受”按钮之前不应执行任何操作。

有解决办法吗?

最佳答案

onCreateDialog 已被弃用。请改用对话 fragment 。优点是显示对话框的代码将从 Activity 中移出,然后您可以显示任何 Activity 的对话框。也动一下

SharedPreferences setting = getSharedPreferences(IConstant.Map_License, 0);
boolean mapLicenseAccept = setting.getBoolean(IConstant.Map_License, false);

像 isLicenceAccepted 这样的实用方法,类似地用于存储数据

 SharedPreferences settings = getSharedPreferences(IConstant.Map_License, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(IConstant.Map_License, true);
editor.commit();

到实用程序中的 acceptLicence 方法。您可以找到如何在对话框 fragment 和您的 Activity 之间进行通信 here .在您的界面而不是 onArticleSelected 中,您将必须实现两种方法 onLicence accepted 和 onLicenceRejected。在您的 Activity 中实现接口(interface)覆盖这两个方法并采取适当的行动。

关于android - 在 android 应用程序中显示许可协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20767991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com