【问题标题】:enabling GPS programmatically not working以编程方式启用 GPS 不起作用
【发布时间】:2012-07-21 22:45:56
【问题描述】:

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

这是我正在使用的代码。

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);

【问题讨论】:

标签: android gps


【解决方案1】:

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

你可以这样做:

startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 100);

【讨论】:

  • 那么像 Go Switch 小部件这样的 3rd 方应用是如何做到的呢?
  • 据我所知,似乎有某种漏洞可以切换对 GPS 数据的访问。但显然这是在 2.3 左右修复的。 SO中已经对这些主题进行了详尽的讨论,并且每个人似乎都得出了相同的结论。如果有可以做到这一点的小部件,它们要么是固件wid,要么利用任何其他类型的安全漏洞,它们不应该这样做。
【解决方案2】:

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

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

但它不起作用

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

【讨论】:

    【解决方案3】:

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

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

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

     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();
    
            }
        }
    

    【讨论】:

      【解决方案4】:
      // location manager
      

      位置管理器位置管理器; LocationListener locationListener;

      obj_tgl_gps.setOnClickListener(新的 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);
                  }
      
              }
          });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-05
        • 2019-09-01
        • 2015-03-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多