位置已开启,但侦听器未返回任何内容

时间:2019-04-09 11:48:25

标签: android arrays location

我需要使用位置管理器获取用户位置。我成功使用意图启动了Settings.ACTION_LOCATION_SOURCE_SETTINGS。我打开了位置信息,但仍然找不到位置。

当我检查电话设置并打开我的应用程序级别权限后,它运行正常。但是我不希望用户手动执行此操作,他们如何才能一次打开位置并同时打开该应用程序的应用程序级别权限

if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||

!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        LocationRequest request = new LocationRequest();
        request.setInterval(0);
        // Build the alert dialog
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Location Services Not Active");
        builder.setMessage("Please enable Location Services and GPS");
        builder.setPositiveButton("OK", new 
DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
                // Show location settings when the user acknowledges the 
alert dialog
                Intent intent = new 
Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
            }
        });
        builder.setNegativeButton("Cancel", new 
DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(EmployeeDashBoard.this,"You must turn on 
Location",Toast.LENGTH_LONG).show();
                startActivity(new Intent(EmployeeDashBoard.this, 
LoginScreen.class));
            }
        });
        Dialog alertDialog = builder.create();
        alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.show();
    }else{
        //listener that responds to location updates
        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                updateEmployeeLocation(location);
            }

            @Override
            public void onStatusChanged(String provider, int status, 
Bundle extras) {
            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {

            }
        };

    }

location turned but no location got 位置已开启,但没有位置 当我打开应用程序级别权限时,它会起作用。用户如何一次执行这两项操作

when i turn on app level permission it works.How can users do both at once

1 个答案:

答案 0 :(得分:1)

我假设您仅在清单中要求LOCATION_PERMISSION,而不在运行时要求。

在Android 6.0(API级别23)之后,您必须在运行时询问权限。

    if (ContextCompat.checkSelfPermission(thisActivity,Manifest.permission.READ_CONTACTS)!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,Manifest.permission.READ_CONTACTS)) {
} else {
    ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.READ_CONTACTS},
            MY_PERMISSIONS_REQUEST_READ_CONTACTS);
}

}

上面的代码是要询问有关READ_CONTACTS的权限 对上面的代码做一些修改并将其添加到您的项目中,它应该可以工作。 浏览此Documentation