在支持Android Cupcake(1.5)的设备上,如何检查和激活GPS?


当前回答

Kotlin解决方案:

private fun locationEnabled() : Boolean {
    val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
    return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
}

其他回答

以下是步骤:

步骤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:如何检查GPS是否启用

 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()
    }

如果用户允许在其设置中使用GPS,则将使用GPS。

你不能再显式地打开它,但你不必这样做——这是一个隐私设置,所以你不想调整它。如果用户同意应用程序获得精确的坐标,它就会被打开。然后,如果可以的话,位置管理器API将使用GPS。

如果你的应用在没有GPS的情况下真的没有用处,而且它是关闭的,你可以在右边的屏幕上打开设置应用,这样用户就可以启用它。

是的,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
            }
        }
    }

这是一种方法,我希望它能有所帮助。 如果我做错了什么,请告诉我。

Kotlin解决方案:

private fun locationEnabled() : Boolean {
    val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
    return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
}