0

ok I got activity that doesent have his layout, it is only there to take gps coordinates and if he succeed then default camera intent is started, if not i show him dialog where user have 2 buttons one to cancel and go back to starting activity and second one is to try getting picture again (calling method for obtaining gps and then sending to camera intent).

The problem is that sometimes when i hit try again button it doesent call getPicture() methond but just continue on like button was never hit nor the dialog was shown and that problem happens often

Here is my code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getPicture();
}

public void getPicture(){



    LocationManager locationManager = null;

    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    locationListener = new MyLocationListener();

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

    if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
        if(MyLocationListener.latitude>0){
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

            startActivityForResult(cameraIntent, CAMERA_REQUEST);


        } else {

            showDialog(GPS_CANT_CONNECT);
        }
    }else{

        showDialog(GPS_OFF);
    }
}

@Override
 protected Dialog onCreateDialog(int id) {
        Dialog d = null;
        switch (id){

        case GPS_OFF: {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Please turn on your GPS before taking picture");
            builder.setNeutralButton("Try Again", new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    getPicture();
                }
            });
            builder.setNegativeButton("Cancel", new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent(StartCameraActivity.this, MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);

                }
            });
            d = builder.create();
            break;
        }
        case GPS_CANT_CONNECT: {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("GPS cant get coordinates! Please make sure that you are outside");
            builder.setNeutralButton("Try Again", new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    getPicture();
                }
            });
            builder.setNegativeButton("Cancel", new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent(StartCameraActivity.this, MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);

                }
            });

            d = builder.create();
            break;
        }

          return d;
}
4

1 回答 1

0

只需添加:

d.show();

在每一d = builder.create();行之后。

因为您只有创建对话框而不显示这就是它不显示的原因。

喜欢:

case GPS_CANT_CONNECT: {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("GPS cant get coordinates! Please make sure that you are outside");
            builder.setNeutralButton("Try Again", new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    getPicture();
                }
            });
            builder.setNegativeButton("Cancel", new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent(StartCameraActivity.this, MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);

                }
            });

            d = builder.create();
            d.show();
            break;
        }
于 2013-04-05T13:17:52.543 回答