我试图通过按钮点击在我的应用程序中以编程方式启用/禁用(切换)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 :(得分:2)
您无法以编程方式启用GPS。您可以做的最多是为用户打开设置。
你可以这样做:
startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 100);
答案 1 :(得分:1)
我正尝试通过点击按钮
在我的应用中以编程方式启用/禁用(切换)GPS
出于明显的隐私原因,这在Android的现代版本中是不可能的。
但它不起作用
Android SDK中没有android.location.GPS_ENABLED_CHANGE
操作。有一个在内部使用,但它只是宣布状态改变 - 它实际上并没有改变状态本身。
答案 2 :(得分: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();
}
}
答案 3 :(得分:0)
// location manager
LocationManager locationManager; LocationListener locationListener;
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);
}
}
});