2012-07-23 44 views
0

我試圖啓用/禁用(切換)GPS在我的應用程序通過點擊按鈕,但它不工作。請幫忙。 我的目標設備未生根。啓用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); 
+0

可能重複的[啓用GPS編程像塔斯克](http://stackoverflow.com/questions/4721449/enable-gps-programatically-like -tasker) – FoamyGuy 2012-07-23 17:07:57

回答

2

您無法以編程方式啓用GPS。你可以做的最多的是打開用戶的設置來做到這一點。

你可以這樣說:

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

然後,像Go Switch小部件這樣的第三方應用怎麼做呢? – narendranathjoshi 2012-07-23 19:09:46

+0

據我所知,似乎有某種利用可以切換對GPS數據的訪問,但顯然這固定在2.3左右。這些主題已經在SO中進行了詳盡的討論,每個人似乎都得出了相同的結論。如果有小部件可以做到這一點,無論是固件wids還是利用任何其他類型的安全缺陷,他們不應該。 – MobileCushion 2012-07-23 20:18:10

1

我想啓用/禁用(切換)編程GPS在通過一個按鈕我的應用程序點擊

這是不可能的現代版本的Android,由於顯而易見的隱私的原因。

,但它不工作

有在Android SDK沒有android.location.GPS_ENABLED_CHANGE行動。有一個在內部使用,但它只是宣佈狀態更改 - 它實際上並沒有改變狀態本身。

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

     } 
    } 
0
// location manager 

的LocationManager的LocationManager; 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); 
      } 

     } 
    });