我有两种方法,一种是要求用户连接到互联网,另一种是连接到gps。我在onresume中调用这两种方法。问题是要求启用gps的alertdialog显示两次,因为onresume再次被调用了,有什么办法解决重复的调用吗?谢谢!
@Override
protected void onResume() {
super.onResume();
enableWifi();
enableGPS();
}
public void enableGPS() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
final AlertDialog.Builder gpsBuilder;
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
gpsBuilder = new AlertDialog.Builder(this)
.setTitle("GPS uitgeschakled")
.setCancelable(false)
.setMessage("Uw GPS is uitgeschakled. Schakled het alstublieft in om door te gaan")
.setPositiveButton("AANZETTEN", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivityForResult(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS), 1);
}
}).setNegativeButton("ANNULEREN", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
gpsBuilder.show();
}
}
public void enableWifi(){
connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager.getActiveNetworkInfo() != null && connectivityManager.getActiveNetworkInfo().isAvailable() && connectivityManager.getActiveNetworkInfo().isConnected()) {
} else {
builder = new AlertDialog.Builder(this);
builder.setMessage("Uw netwerk is uitgeschakeld.Schakel het alstublieft in om door te gaan")
.setTitle("Netwerk is uitgeschakeld")
.setCancelable(false)
.setPositiveButton("AANZETTEN", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
}
})
.setNegativeButton("ANNULEREN", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
});
builder.show();
}
}
}
而不是像上面那样使用alertdialog,请考虑使用DialogFragment。
您的AlertDialog类将类似于:
public class AlertDialogFragment extends DialogFragment {
/** Create a new instance of the Dialog Fragment which will display an alert dialog. The alert
* dialog is always cancelable.
* @param title The title of the alert dialog.
* @param message The message.
* @return An alert dialog.
*/
public static AlertDialogFragment newInstance(String title, String message) {
AlertDialogFragment frag = new AlertDialogFragment();
Bundle args = new Bundle();
args.putString("title", title);
args.putString("msg", message);
frag.setArguments(args);
return frag;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
String title = getArguments().getString("title");
String message = getArguments().getString("msg");
View v = inflater.inflate(R.layout.fragment_alert_dialog, container, false);
setCancelable(false);
if (getDialog() != null && getDialog().getWindow() != null) {
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().setCanceledOnTouchOutside(false);
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
TextView titleArea = v.findViewById(R.id.title);
titleArea.setText(title);
TextView messageArea = v.findViewById(R.id.message);
messageArea.setText(message);
Button okButton = v.findViewById(R.id.button);
okButton.setText(R.string.ok);
okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
return v;
}
要显示对话框,您可以执行以下操作:
AlertDialogFragment dialog = (AlertDialogFragment) fragmentManager.findFragmentByTag(tag);
if (dialog == null) {
dialog = UpdatingProgressDialogFragment.newInstance(message);
dialog.show(fragmentManager, tag);
}
其中“标记”将是该对话框的标识字符串,例如。 “ ALERT”。
注:“ fragment_alert_dialog”只是一个布局文件,定义警报对话框的外观。
第二注:使用片段的另一个优点是,旋转设备时,对话框不会关闭。
响应按钮的点击:抱歉,我省略了这一部分。要响应按钮单击,可以为对话框片段定义一个接口。例如
/** Should be implemented by all users of this dialog. */
public interface DialogUser {
/**
* Will be called when the YES option was selected.
* @param tag The Dialog Tag, for when there is more than one dialog in the activity.
**/
void onDialogYesSelected(String tag);
/**
* Will be called when the NO option was selected.
* @param tag The Dialog Tag, for when there is more than one dialog in the activity.
**/
void onDialogNoSelected(String tag);
}
然后,当用户按下其中一个按钮时,您可以执行:
yesButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (getActivity() instanceof DialogUser) {
((YesNoDialogUser) getActivity()).onDialogYesSelected(mTag);
} else {
Log.w("DIALOG", "activity does not implement interface");
}
dismiss();
}
});
然后,调用对话框的活动应扩展DialogUser并定义该触发器应发生的情况。您还可以将一个特殊的字符串传递给onDialogYesSelected()中使用的对话框,以便知道哪个对话框触发了该特定的onDialogYesSelected()调用-例如,如果您的活动中有一个针对Wifi的对话框,另一个针对GPS的对话框,则每一个都会给onDialogYesSelected()]提供一个不同的标签