当满足某些条件时,如何防止出现警报对话框


//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", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, 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", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, 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();
}

如果用户对"警报"对话框选择"否",它将不会再次出现。

相关内容

  • 没有找到相关文章

最新更新