在Android应用程序中显示许可协议

时间:2013-12-25 00:09:49

标签: android

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

1放置此对话框的位置?

添加其他活动或将对话框放在启动活动MainActivity处?

2如果用户点击“拒绝”

,如何关闭应用程序

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


根据“Ahmad”的回答,我将决定是否在活动开始时打开一个对话框(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();

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

有什么想法来解决它吗?

2 个答案:

答案 0 :(得分:0)

  

放置此对话框的位置?

当用户第一次打开应用时,就在开头。如果已经显示此对话框,则通过将其保存在共享首选项中来跟踪它。您不必为此创建单独的活动。你可以,但我见过的大多数应用都没有。

  

如果用户点击"拒绝"

,如何关闭应用

完成活动并将其保存在您的共享偏好设置中。因此,每次用户打开您的应用程序时,您都会检查" hasUserAcceptedOurAgreement"的布尔值。是真是假,依赖于此。


我只是从技术角度回答如何可靠地完成这项工作。我不是律师,但就我所知,将许可协议提交给游戏商店是完全有效的,因此可以从应用程序应用程序页面获取(为什么会有这个选项?)。

答案 1 :(得分:0)

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

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方法。 您可以找到如何在对话框片段和您的活动here之间进行通信。在您的界面而不是onArticleSelected中,您必须实现两个onLicence接受和onLicenceRejected方法。在您的活动中实现接口覆盖这两种方法并采取适当的措施。