【发布时间】:2019-01-04 05:20:25
【问题描述】:
我有一个带有自定义布局的警报对话框,当 GPS 或网络未启用时,它会弹出,以便用户启用它们。
代码如下:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get Location Manager and check for GPS & Network location services
final LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER) || !lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
View mView = getLayoutInflater().inflate(R.layout.activity_gps_alert_dialog, null);
Button positiveBtn = (Button) mView.findViewById(R.id.positiveBtn);
final Button negativeBtn = (Button) mView.findViewById(R.id.negativeBtn);
builder.setView(mView);
final AlertDialog dialog = builder.create();
positiveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Show location settings when the user acknowledges the alert dialog
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
negativeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
System.exit(0);
}
});
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
问题在于,即使启用了 GPS 和网络,即使用户通过它启用了这些弹出对话框,此弹出对话框仍在屏幕上。
【问题讨论】:
标签: android location android-alertdialog