0

我正在尝试通过单击按钮在我的应用程序中以编程方式启用/禁用(切换)GPS,但它不起作用。请帮忙。我的目标设备没有植根。

这是我正在使用的代码。

private void gps()
{
Intent intent=new Intent("android.location.GPS_ENABLED_CHANGE");
Button gps = (Button)findViewById(R.id.gpsButton);
if(isGPSon())
    {
    intent.putExtra("enabled", true);
    gps.setText(R.string.f2_gps_deact);
    }
else
    {
    intent.putExtra("enabled", false);
    gps.setText(R.string.f2_gps_act);
    }
sendBroadcast(intent);
4

4 回答 4

2

您不能以编程方式启用 GPS。您最多可以打开设置供用户执行此操作。

你可以这样做:

startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 100);
于 2012-07-23T16:58:09.700 回答
1

我正在尝试通过单击按钮在我的应用程序中以编程方式启用/禁用(切换)GPS

由于明显的隐私原因,这在现代版本的 Android 上是不可能的。

但它不工作

Android SDK中没有任何android.location.GPS_ENABLED_CHANGE操作。内部使用了一个,但它只是宣布状态更改——它实际上并没有更改状态本身。

于 2012-07-23T16:56:53.640 回答
1

您可以强制您的应用查看是否收到 GPS 信号。如果您没有收到信号,您可以进行选择对话框,以便用户可以选择进入设置并打开 gps。

// GPSTracker class
GPSTracker gps;

// CHECK GPS!
gps = new GPSTracker(YourActivityClass.this);
if (gps.canGetLocation()) {
} else {
    gps.showSettingsAlert();

        }

然后新建一个类文件,把这段代码粘贴进去。。这段代码我没有写。我在某个地方找到它,不记得我从哪里得到它。

 import android.app.AlertDialog;
 import android.app.Service;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.location.Location;
 import android.location.LocationListener;
 import android.location.LocationManager;
 import android.os.Bundle;
 import android.os.IBinder;
 import android.provider.Settings;
 import android.util.Log;

 public class GPSTracker extends Service implements LocationListener {

private final Context mContext;

// flag for GPS status
boolean isGPSEnabled = false;

// flag for network status
boolean isNetworkEnabled = false;

// flag for GPS status
boolean canGetLocation = false;

Location location; // location
double latitude; // latitude
double longitude; // longitude

// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

// Declaring a Location Manager
protected LocationManager locationManager;

public GPSTracker(Context context) {
    this.mContext = context;
    getLocation();
}

public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled || !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("GPS Enabled", "GPS Enabled");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            if (isNetworkEnabled) {
                if (locationManager == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}

/**
 * Stop using GPS listener
 * Calling this function will stop using GPS in your app
 * */
public void stopUsingGPS(){
    if(locationManager != null){
        locationManager.removeUpdates(GPSTracker.this);
    }
}

/**
 * Function to get latitude
 * */
public double getLatitude(){
    if(location != null){
        latitude = location.getLatitude();
    }

    // return latitude
    return latitude;
}

/**
 * Function to get longitude
 * */
public double getLongitude(){
    if(location != null){
        longitude = location.getLongitude();
    }

    // return longitude
    return longitude;
}

/**
 * Function to check GPS/wifi enabled
 * @return boolean
 * */
public boolean canGetLocation() {
    return this.canGetLocation;
}

/**
 * Function to show settings alert dialog
 * On pressing Settings button will lauch Settings Options
 * */
public void showSettingsAlert(){
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

    // Setting Dialog Title
    alertDialog.setTitle("GPS settings");

    // Setting Dialog Message
    alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

    // On pressing Settings button
    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int which) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            mContext.startActivity(intent);
        }
    });

    // on pressing cancel button
    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
        }
    });

    // Showing Alert Message
    alertDialog.show();
}

@Override
public void onLocationChanged(Location location) {
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

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

@Override
public IBinder onBind(Intent arg0) {
    return null;
}
 }

在我的应用程序中,我输入了共享首选项,以便用户可以决定是否要检查 gps。你可以这样做..

        SharedPreferences getData = PreferenceManager
            .getDefaultSharedPreferences(getBaseContext());
    String enableGPS = getData.getString("gps", "true");
    boolean enableGPSboolean = Boolean.parseBoolean(enableGPS);

    if (enableGPSboolean == (true)) {

        // CHECK GPS!
        // create class object
        gps = new GPSTracker(AddFish.this);

        if (gps.canGetLocation()) {

        } else {

            gps.showSettingsAlert();

        }
    }
于 2012-07-23T17:48:43.317 回答
0
// location manager

位置管理器位置管理器;位置监听器位置监听器;

obj_tgl_gps.setOnClickListener(new OnClickListener() {

        public void onClick(View v)
        {
            if (obj_tgl_gps.isChecked())
            {
                Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
                intent.putExtra("enabled", true);
                sendBroadcast(intent);
            }
            else
            {
                Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
                intent.putExtra("enabled", false);
                sendBroadcast(intent);
            }

        }
    });
于 2013-04-09T06:17:40.157 回答