如何解决满足某些条件时如何防止出现警报对话框?
//NotificationPermission Dialog
private void notificationPermissionDialog(){
AlertDialog dialog = new AlertDialog.Builder(getActivity())
.setCancelable(false)
.setMessage("Do you want to Enable Notifications?")
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface,int i) {
savePermissionInPreferences("Yes");
dialogInterface.dismiss();
}
}).setNegativeButton("No",int i) {
savePermissionInPreferences("No");
dialogInterface.dismiss();
}
}).show();
}
//Save Notification Permission in Preferences
private void savePermissionInPreferences(String isNotificationAllowed){
SharedPreferences sharedPreferences;
sharedPreferences = getContext().getSharedPreferences("PermissionPref",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("isNotificationAllowed",isNotificationAllowed);
editor.apply();
}
我创建了一个对话框,该对话框在打开片段时出现并请求许可,如果我单击“否”,它的值将保存在共享首选项中以防止它再次出现,但是当我在同一活动中替换我的片段时,对话框一次又一次地出现.请任何人都可以指导我完成它。 提前致谢。
解决方法
所以你可以做类似下面的事情
private void notificationPermissionDialog(){
SharedPreferences sharedPreferences = getSharedPreferences("PermissionPref",Context.MODE_PRIVATE);//Add these
String permission = sharedPreferences.getString("isNotificationAllowed","");
//Add this line
if(!permission == "No"){
AlertDialog dialog = new AlertDialog.Builder(getActivity())
.setCancelable(false)
.setMessage("Do you want to Enable Notifications?")
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface,int i) {
savePermissionInPreferences("Yes");
dialogInterface.dismiss();
}
}).setNegativeButton("No",int i) {
savePermissionInPreferences("No");
dialogInterface.dismiss();
}
}).show();
}
}
//Save Notification Permission in Preferences
private void savePermissionInPreferences(String isNotificationAllowed){
SharedPreferences sharedPreferences;
sharedPreferences = getContext().getSharedPreferences("PermissionPref",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("isNotificationAllowed",isNotificationAllowed);
editor.apply();
}
如果用户对警报对话框选择否,它就不会再次出现。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。