模拟器上未显示Android Alert对话框

时间:2017-02-11 16:40:36

标签: android gps alertdialog

我编写代码来检查GPS设置并发出警告对话框,但它并没有显示在Android模拟器中。

这是用于检查GPS设置并显示警告对话框的代码。

   package com.example.user.testlocation;

    import android.content.DialogInterface;
    import android.content.Intent;
    import android.location.LocationManager;
    import android.provider.Settings;
    import android.support.v7.app.AlertDialog;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;

    public class Location extends AppCompatActivity {


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_location);
        }

        private void isLocationEnalbled(){
            LocationManager locationManager=(LocationManager)getSystemService(LOCATION_SERVICE);
            if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)|| !locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
                AlertDialog.Builder alertDialog=new AlertDialog.Builder(this);
                alertDialog.setTitle("Enable Location");
                alertDialog.setMessage("Your locations setting is not enabled. Please enabled it in settings menu.");
                alertDialog.setPositiveButton("Location Settings", new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog, int which){
                        Intent intent=new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        startActivity(intent);
                    }
                });
                alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog, int which){
                        dialog.cancel();
                    }
                });
                AlertDialog alert=alertDialog.create();
                alert.show();
            }
            else{
                AlertDialog.Builder alertDialog=new AlertDialog.Builder(this);
                alertDialog.setTitle("Confirm Location");
                alertDialog.setMessage("Your Location is enabled, please enjoy");
                alertDialog.setNegativeButton("Back to interface",new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog, int which){
                        dialog.cancel();
                    }
                });
                AlertDialog alert=alertDialog.create();
                alert.show();
            }
        }
    }

它没有显示任何错误,但是当我实施它时,警告对话框不会显示。

2 个答案:

答案 0 :(得分:0)

您永远不会调用进行检查的isLocationEnalbled()方法。将此添加到您的班级,以便每次活动resumed时应用都会检查isLocationEnalbled()

@Override
public void onResume() {
    super.onResume();
    isLocationEnalbled();
}

答案 1 :(得分:0)

在你的onCreate()方法中,你必须调用isLocationEnalbled()

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);
        isLocationEnalbled()
    }