Android使用AlertDialog激活gps:如何等待用户采取行动?

前端之家收集整理的这篇文章主要介绍了Android使用AlertDialog激活gps:如何等待用户采取行动?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我读过不同的帖子,因为阻止了用户界面,所以当显示AlertDialog时,无法等待用户采取操作.

但是,像Facebook这样的应用程序显示Gps目前被禁用.要启用gps吗?提醒对话框,等待用户按是/否.
我认为可以使用2种不同的活动,第一种只包含gps警报对话框,但这似乎并不正确,并且绝对不会像Facebook这样做.

任何人都可以告诉我如何实现这一点?

这是我的代码

@Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    InitializeComponents();

    EnableGPSIfPossible();


        ListAsync lAsync = new ListAsync(this);
        lAsync .execute();
    }


    private void EnableGPSIfPossible()
{   
    final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
     if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
            buildAlertMessageNoGps();
        }
}

 private  void buildAlertMessageNoGps() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Yout GPS seems to be disabled,do you want to enable it?")
               .setCancelable(false)
               .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                   public void onClick(@SuppressWarnings("unused") final DialogInterface dialog,@SuppressWarnings("unused") final int id) {
                     startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                   }
               })
               .setNegativeButton("No",new DialogInterface.OnClickListener() {
                   public void onClick(final DialogInterface dialog,@SuppressWarnings("unused") final int id) {
                        dialog.cancel();
                   }
               });
        final AlertDialog alert = builder.create();
        alert.show();
    }

所以,在我的代码中,AsynTask是imediately启动的,而不用等待用户激活gps. (这是一个正常的行为)

谢谢!

解决方法

你可以做的是这样的:

GPSManager.java:

public class GPSManager {

    private Activity activity;
    private LocationManager mlocManager;
    private LocationListener gpsListener;

    public GPSManager(Activity activity) {
        this.activity = activity;
    }

    public void start() {
        mlocManager = (LocationManager) activity
                .getSystemService(Context.LOCATION_SERVICE);

        if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            setUp();
            findLoc();
        } else {
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    activity);
            alertDialogBuilder
                    .setMessage("GPS is disabled in your device. Enable it?")
                    .setCancelable(false)
                    .setPositiveButton("Enable GPS",new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,int id) {
                                    Intent callGPSSettingIntent = new Intent(
                                            android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                    activity.startActivity(callGPSSettingIntent);
                                }
                            });
            alertDialogBuilder.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {
                            dialog.cancel();
                        }
                    });
            AlertDialog alert = alertDialogBuilder.create();
            alert.show();

        }
    }

    public void setUp() {
        gpsListener = new GPSListener(activity,mlocManager);
    }

    public void findLoc() {
        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1,gpsListener);

        if (mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER) == null)
            Toast.makeText(activity,"LAST Location null",Toast.LENGTH_SHORT)
                    .show();
        else {
            gpsListener.onLocationChanged(mlocManager
                    .getLastKnownLocation(LocationManager.GPS_PROVIDER));
        }
    }
}

GPSListener.java:

public class GPSListener implements LocationListener {

    private Activity activity;
    private LocationManager lm;
    private int numberOfUpdates;

    public static final int MAX_NUMBER_OF_UPDATES = 10;

    public GPSListener(Activity activity,LocationManager lm) {
        this.activity = activity;
        this.lm = lm;
    }

    @Override
    public void onLocationChanged(Location loc) {
        if (numberOfUpdates < MAX_NUMBER_OF_UPDATES) {
            numberOfUpdates++;

            Log.w("LAT",String.valueOf(loc.getLatitude()));
            Log.w("LONG",String.valueOf(loc.getLongitude()));
            Log.w("ACCURACY",String.valueOf(loc.getAccuracy() + " m"));
            Log.w("PROVIDER",String.valueOf(loc.getProvider()));
            Log.w("SPEED",String.valueOf(loc.getSpeed() + " m/s"));
            Log.w("ALTITUDE",String.valueOf(loc.getAltitude()));
            Log.w("BEARING",String.valueOf(loc.getBearing() + " degrees east of true north"));

            String message;

            if (loc != null) {
                message = "Current location is:  Latitude = "
                        + loc.getLatitude() + ",Longitude = "
                        + loc.getLongitude();
                // lm.removeUpdates(this);
            } else
                message = "Location null";

            Toast.makeText(activity,message,Toast.LENGTH_SHORT).show();
        } else {
            lm.removeUpdates(this);
        }
    }

    @Override
    public void onProviderDisabled(String provider) {
        Toast.makeText(activity,"Gps Disabled",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onProviderEnabled(String provider) {
        Toast.makeText(activity,"Gps Enabled",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStatusChanged(String provider,int status,Bundle extras) {

    }

}

然后从你的活动:

GPSManager gps = new GPSManager(
                                    yourActivity.this);
                            gps.start();
原文链接:https://www.f2er.com/android/312651.html

猜你在找的Android相关文章