Answers:
最好的方法似乎如下:
final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
buildAlertMessageNoGps();
}
private void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
alert
对整个活动进行声明,以便您可以在onDestroy中将其关闭以避免内存泄漏(if(alert != null) { alert.dismiss(); }
)
LocationManager.NETWORK_PROVIDER
将返回true
在android中,我们可以使用LocationManager轻松检查设备中是否启用了GPS。
这是要检查的简单程序。
GPS是否启用:-在AndroidManifest.xml中将以下用户权限行添加到访问位置
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
您的Java类文件应为
public class ExampleApp extends Activity {
/** Called when the activity is first created. */
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
Toast.makeText(this, "GPS is Enabled in your devide", Toast.LENGTH_SHORT).show();
}else{
showGPSDisabledAlertToUser();
}
}
private void showGPSDisabledAlertToUser(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("GPS is disabled in your device. Would you like to enable it?")
.setCancelable(false)
.setPositiveButton("Goto Settings Page To Enable GPS",
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
Intent callGPSSettingIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(callGPSSettingIntent);
}
});
alertDialogBuilder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
dialog.cancel();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
}
输出看起来像
alert
整个活动,以便您可以将其关闭onDestroy()
以避免内存泄漏(if(alert != null) { alert.dismiss(); }
)
是的,不能再通过编程方式更改GPS设置,因为它们是隐私设置,我们必须从程序中检查是否已将其打开,如果未打开,则应进行处理。您可以通知用户GPS已关闭,并根据需要使用类似的方法向用户显示设置屏幕。
检查位置提供者是否可用
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(provider != null){
Log.v(TAG, " Location providers: "+provider);
//Start searching for location and update the location text when update available
startFetchingLocation();
}else{
// Notify users and show settings if they want to enable GPS
}
如果用户要启用GPS,则可以以这种方式显示设置屏幕。
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, REQUEST_CODE);
在onActivityResult中,您可以查看用户是否启用了它
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == REQUEST_CODE && resultCode == 0){
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(provider != null){
Log.v(TAG, " Location providers: "+provider);
//Start searching for location and update the location text when update available.
// Do whatever you want
startFetchingLocation();
}else{
//Users did not switch on the GPS
}
}
}
那是做到这一点的一种方法,希望对您有所帮助。让我知道我做错了什么。
startActivityForResult
多个意图。当意图返回到您的活动时,您检查RequestCode以查看返回的意图并做出相应的响应。
provider
可以是一个空字符串。我不得不将支票更改为(provider != null && !provider.isEmpty())
步骤如下:
步骤1:创建在后台运行的服务。
步骤2:您也需要清单文件中的以下权限:
android.permission.ACCESS_FINE_LOCATION
步骤3:编写代码:
final LocationManager manager = (LocationManager)context.getSystemService (Context.LOCATION_SERVICE );
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) )
Toast.makeText(context, "GPS is disabled!", Toast.LENGTH_LONG).show();
else
Toast.makeText(context, "GPS is enabled!", Toast.LENGTH_LONG).show();
第4步:或者您可以使用以下方法进行检查:
LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE );
boolean statusOfGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
步骤5:连续运行服务以监视连接。
此方法将使用LocationManager服务。
源链接
//Check GPS Status true/false
public static boolean checkGPSStatus(Context context){
LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE );
boolean statusOfGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
return statusOfGPS;
};
如果用户允许在其设置中使用GPS,则将使用GPS。
您无法再明确启用此功能,但不必这样做-这确实是一项隐私设置,因此您不想对其进行调整。如果用户对应用程序获得精确的坐标感到满意,它将打开。然后,如果可以的话,位置管理器API将使用GPS。
如果您的应用程序在没有GPS的情况下确实没有用,并且已关闭,则可以使用意图在右侧屏幕上打开设置应用程序,以便用户启用它。
在您的中LocationListener
,实现onProviderEnabled
和onProviderDisabled
事件处理程序。呼叫时requestLocationUpdates(...)
,如果手机上的GPS禁用,onProviderDisabled
将被呼叫;如果用户启用GPS,onProviderEnabled
将被调用。
In Kotlin: - How to check GPS is enable or not
val manager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
checkGPSEnable()
}
private fun checkGPSEnable() {
val dialogBuilder = AlertDialog.Builder(this)
dialogBuilder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
.setCancelable(false)
.setPositiveButton("Yes", DialogInterface.OnClickListener { dialog, id
->
startActivity(Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS))
})
.setNegativeButton("No", DialogInterface.OnClickListener { dialog, id ->
dialog.cancel()
})
val alert = dialogBuilder.create()
alert.show()
}