【问题标题】:How do I check if Location is turned on or off in my Kotlin Android app? [duplicate]如何检查我的 Kotlin Android 应用中的 Location 是打开还是关闭? [复制]
【发布时间】:2020-05-21 13:45:05
【问题描述】:

我的应用包含以下代码来检查使用设备位置服务的权限,但我需要某种方法来检测设备中的 Location 是否打开或关闭,如果它关闭则将其打开。

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_counties)

//        selectedCounty = intent.getStringExtra("COUNTY")!!

        // Custom action bar code to return to list of counties
//        configureCustomActionBar()

        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) !=
            PackageManager.PERMISSION_GRANTED) {

            Log.d("Debug","Permission not granted")

            // Permission is not granted
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    android.Manifest.permission.ACCESS_FINE_LOCATION)) {

                Toast.makeText(this, "Location needed for navigation", Toast.LENGTH_SHORT).show()

            } else {

                Log.d("Debug","Request Permission")

                // No explanation needed, we can request the permission.
                ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION), REQUEST_CODE)
            }
        } else {

            // Permission has already been granted
            Log.d("Debug", "Permission already granted")

        }

    }

虽然已授予权限,但我仍然需要某种方式打开 Location 或提示用户手动打开它。

【问题讨论】:

  • @greeble31 可以,谢谢。

标签: android kotlin permissions location


【解决方案1】:

最终的 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();

}

【讨论】:

  • 感谢您的帮助。
【解决方案2】:
            val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager

            if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) == false) {

                // Alert user to switch Location on in Settings
                val builder = AlertDialog.Builder(this)

                // Set the alert dialog title
                builder.setTitle("Turn on \"Location\"")

                // Display a message on alert dialog
                builder.setMessage("\"Location\" is currently turned off. Turn on \"Location\" to enable navigation function in GoSiteUK")

                // Set a positive button and its click listener on alert dialog
                builder.setPositiveButton("OK"){dialog, which ->

                    val intent = Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)
                    startActivity(intent)
                }

                // Display a negative button on alert dialog
                builder.setNegativeButton("Cancel"){dialog,which ->
                    val intent = Intent(this, CountriesActivity::class.java)
                    startActivity(intent)

                }

                // Finally, make the alert dialog using builder
                val dialog: AlertDialog = builder.create()

                // Display the alert dialog on app interface
                dialog.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-09
    • 2015-10-05
    • 2011-10-20
    • 2013-03-11
    • 1970-01-01
    • 2021-06-17
    相关资源
    最近更新 更多