gpt4 book ai didi

java - 我的 GPS 提醒不断要求我启用 GPS

转载 作者:行者123 更新时间:2023-12-01 14:56:50 27 4
gpt4 key购买 nike

我在我的应用程序中提供了 GPS 提醒,但是当我启用 GPS 并返回到我的 mainactivty 时,当我按下 googlemaps 按钮时,我的 GPS 提醒会再次询问我。仅当我关闭程序并重新启动应用程序时它才有效

这是我的 MainActivityClass。

GPSTracker gps;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gps = new GPSTracker(MainActivity.this);
}

我的谷歌地图按钮。

public void googlemaps(View view) {
if (gps.canGetLocation()) {
Intent intent = new Intent(this, GoogleMaps.class);
startActivity(intent);
} else {
gps.showSettingsAlert();
}
}

这是我的 GPS 跟踪器类

public class GPSTracker extends Service implements LocationListener {

private final Context mContext;

// flag for GPS status
boolean isGPSEnabled = false;

// flag for GPS status
boolean canGetLocation = false;

Location location; // location

public LocationManager locationManager;

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

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

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

if (!isGPSEnabled) {
} else {
this.canGetLocation = true;

// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 10, 2000, this);

Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
}
}
}
}

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

return location;
}


/**
* 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 is 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() {
@Override
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() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});

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

@Override
public void onLocationChanged(Location loc) {
}

@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 是否已启用。从表面上看,您在创建类时设置了 canGetLocation 变量(通过调用 getLocation())。添加对 'getLocation() 的调用>inonResume()` 应该可以解决您的问题。我建议打破逻辑以查看 GPS 是否已启用到其自己的方法中。

关于java - 我的 GPS 提醒不断要求我启用 GPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14248864/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com