在支持Android Cupcake(1.5)的设备上,如何检查和激活GPS?
当前回答
如果用户允许在其设置中使用GPS,则将使用GPS。
你不能再显式地打开它,但你不必这样做——这是一个隐私设置,所以你不想调整它。如果用户同意应用程序获得精确的坐标,它就会被打开。然后,如果可以的话,位置管理器API将使用GPS。
如果你的应用在没有GPS的情况下真的没有用处,而且它是关闭的,你可以在右边的屏幕上打开设置应用,这样用户就可以启用它。
其他回答
最好的方法似乎如下:
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();
}
以下是步骤:
步骤1:创建在后台运行的服务。
步骤2:在Manifest文件中也需要以下权限:
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();
第四步:或者你可以简单地使用:
LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE );
boolean statusOfGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
步骤5:持续运行服务以监控连接。
Kotlin解决方案:
private fun locationEnabled() : Boolean {
val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
}
是的,你可以检查以下是代码:
public boolean isGPSEnabled (Context mContext){
LocationManager locationManager = (LocationManager)
mContext.getSystemService(Context.LOCATION_SERVICE);
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
如果用户允许在其设置中使用GPS,则将使用GPS。
你不能再显式地打开它,但你不必这样做——这是一个隐私设置,所以你不想调整它。如果用户同意应用程序获得精确的坐标,它就会被打开。然后,如果可以的话,位置管理器API将使用GPS。
如果你的应用在没有GPS的情况下真的没有用处,而且它是关闭的,你可以在右边的屏幕上打开设置应用,这样用户就可以启用它。
推荐文章
- createScaledBitmap的过滤器参数做什么?
- 为什么我在使用adb时访问数据文件夹被拒绝?
- 如何确定Android设备的GPS是否已启用
- 没有找到与给定名称匹配的资源:attr 'android:keyboardNavigationCluster'。更新到支持库26.0.0时
- 禁用在android中打开导航抽屉的滑动手势
- Android:允许平板电脑竖屏和横屏,但强制手机竖屏?
- 如何使布局与视图填充剩余的空间?
- 字符串资源新行/n不可能?
- 如何点击或点击一个TextView文本
- Android房间-简单的选择查询-不能访问数据库在主线程
- Android: ScrollView vs NestedScrollView
- 错误:无法运行mksdcard SDK工具
- 未附加到窗口管理器的视图崩溃
- 将焦点设置为EditText
- 如何把空格字符放入XML字符串名?