使用 com.google.android.gms.location.LocationListener 时要求用户激活其 GPS
Asking the user to activate its GPS when using com.google.android.gms.location.LocationListener
我研究了所有与此相关的 post 并阅读了很多文档,但我仍然不知道该怎么做。当我 运行 我的 phone 上的应用程序添加了位置按钮,但如果未启用 GPS,则按下它时没有任何反应。当我启用它时(手动,没有被提示或任何东西)它工作得很好。
我想要做的是,当用户按下标准位置按钮时,应该调用一个新的自定义意图。像这样:
if (mLastLocation == null) {
LayoutInflater inflater = getLayoutInflater();
@SuppressLint("InflateParams") View layout = inflater.inflate(R.layout.toast_gps_view, null);
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
当我导入 android.location.LocationListener;回调 onProviderDisabled 方法被调用,我可以在那里添加我的代码,但现在使用 com.google.android.gms.location.LocationListener 该方法不再被覆盖。
那么我需要做什么才能提示用户激活 GPS?
终于知道怎么做了。这是我的做法:
mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
@Override
public boolean onMyLocationButtonClick() {
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabledGPS = service
.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean enabledWiFi = service
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!enabledGPS) {
LayoutInflater inflater = getLayoutInflater();
@SuppressLint("InflateParams") View layout = inflater.inflate(R.layout.toast_gps_view, null);
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
} else if (!enabledWiFi) {
Toast.makeText(getApplicationContext(),
"Network signal or WiFi connections not found",
Toast.LENGTH_LONG).show();
Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
return false;
}
});
我研究了所有与此相关的 post 并阅读了很多文档,但我仍然不知道该怎么做。当我 运行 我的 phone 上的应用程序添加了位置按钮,但如果未启用 GPS,则按下它时没有任何反应。当我启用它时(手动,没有被提示或任何东西)它工作得很好。
我想要做的是,当用户按下标准位置按钮时,应该调用一个新的自定义意图。像这样:
if (mLastLocation == null) {
LayoutInflater inflater = getLayoutInflater();
@SuppressLint("InflateParams") View layout = inflater.inflate(R.layout.toast_gps_view, null);
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
当我导入 android.location.LocationListener;回调 onProviderDisabled 方法被调用,我可以在那里添加我的代码,但现在使用 com.google.android.gms.location.LocationListener 该方法不再被覆盖。 那么我需要做什么才能提示用户激活 GPS?
终于知道怎么做了。这是我的做法:
mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
@Override
public boolean onMyLocationButtonClick() {
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabledGPS = service
.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean enabledWiFi = service
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!enabledGPS) {
LayoutInflater inflater = getLayoutInflater();
@SuppressLint("InflateParams") View layout = inflater.inflate(R.layout.toast_gps_view, null);
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
} else if (!enabledWiFi) {
Toast.makeText(getApplicationContext(),
"Network signal or WiFi connections not found",
Toast.LENGTH_LONG).show();
Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
return false;
}
});