无法打开android api23上的位置? [英] Can't turn on location on android api23?

查看:120
本文介绍了无法打开android api23上的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用了以下代码以及按预期要求许可显示的对话。但是当我点击允许时,它什么也不做。日志消息不会显示,因为如果权限未被授予,所以我去我的参数来验证位置是开,它是关。是不是应该开启,因为我授予应用程序访问我的位置?
如果我手动将其打开并再次运行应用程序,一旦它请求我的许可,它就会起作用并显示日志消息,但并不是要求权限(通过对话)转向在用户点击允许的位置(何时关闭)?
我做错了什么?我应该提及,我在api23上运行应用程序



是我的创建中的代码:

  mApiClient =新GoogleApiClient.Builder(本)
.addConnectionCallbacks(本)
.addOnConnectionFailedListener(本)
.addApi( LocationServices.API)
.build();

mApiClient.connect();

//创建LocationRequest对象
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(10 * 1000)// 10秒,以毫秒为单位
.setFastestInterval(1 * 1000); // 1秒,以毫秒为单位

这是我的 OnConnected 方法:

  public void onConnected(@Nullable Bundle bundle){
//启动服务
//检查并要求许可

如果(ActivityCompat.checkSelfPermission(这一点,Manifest.permission.ACCESS_FINE_LOCATION)= PackageManager.PERMISSION_GRANTED和放大器;!&安培;!ActivityCompat.checkSelfPermission(这一点,Manifest.permission.ACCESS_COARSE_LOCATION)= PackageManager .PERMISSION_GRANTED){
如果(ContextCompat.checkSelfPermission(这一点,Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(这一点,
新的String [] {清单。 permission.ACCESS_FINE_LOCATION},
MY_PERMISSION_ACCESS_FINE_LOCATION);
}
// public void onRequestPermissionsResult(int requestCode,String [] permissions,
// int [] grantResults)
//处理用户授予权限的情况。有关更多详细信息,请参阅文档
//获取ActivityCompat#requestPermissions。
return;
}
位置位置= LocationServices.FusedLocationApi.getLastLocation(mApiClient);

if(location == null){
LocationServices.FusedLocationApi.requestLocationUpdates(mApiClient,mLocationRequest,this);

} else {
//如果一切正常,让我们获取经纬度
currentLatitude = location.getLatitude();
currentLongitude = location.getLongitude();
Log.v(currentLatitude,currentLatitude +WORKS+ currentLongitude +);
}

}


解决方案

试试这段代码:

  private LocationCoord gps = null; 
private static final int PERMISSION_REQUEST_CODE = 1;

在OnCreate()中:

  // GPS管理
LocationManager lm =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
布尔值gps_enabled = false;
布尔值network_enabled = false;

尝试{
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch(Exception ex){
}

try {
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch(Exception ex){
}

if(!gps_enabled&&!network_enabled){
//通知用户
AlertDialog。 Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage(允许ImHere访问此设备的位置?);
dialog.setPositiveButton( 允许,新DialogInterface.OnClickListener(){
@覆盖
公共无效的onClick(DialogInterface paramDialogInterface,诠释paramInt){
// TODO自动生成方法存根
Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(myIntent);
// get gps
}
});
dialog.setNegativeButton( 拒绝,新DialogInterface.OnClickListener(){

@覆盖
公共无效的onClick(DialogInterface paramDialogInterface,诠释paramInt){
// TODO自动生成方法存根

}
});
dialog.show();
}

gps = new LocationCoord(this);






  @覆盖
protected void onStart(){
super.onStart();

//权限android 6.0
if(!checkPermission()){
requestPermission();




$ b私有布尔值checkPermission(){
int result = ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION );
if(result == PackageManager.PERMISSION_GRANTED)return true;
else返回false;
}

private void requestPermission(){
ActivityCompat.requestPermissions(this,new String [] {Manifest.permission.ACCESS_FINE_LOCATION},PERMISSION_REQUEST_CODE);
}

您需要Manifest.xml的这个权限:

 < uses-permission android:name =android.permission.CHANGE_WIFI_STATE/> 
< uses-permission android:name =android.permission.INTERNET/>
< uses-permission android:name =android.permission.ACCESS_FINE_LOCATION/>
< uses-permission android:name =android.permission.ACCESS_NETWORK_STATE/>
<使用权限android:name =android.permission.ACCESS_WIFI_STATE/>

您可以在这里获取LoocationCord.java: https://github.com/toomyy94/ImHere -Chatbot / blob / master / app / src / main / java / pt / ua / tomasr / imhere / modules / LocationCoord.java

I have used the following code and the dialogue that asks for permission shows as expected. But when I click "allow" it doesn't do anything. The log message doesn't appear as if the permission wasn't granted so I went to my parameters to verify if location is "on" and it was "off". Wasn't it supposed to be on because I granted the app access to my location ? If I manually turn it "on" and then run the app again, once it asks for my permission, it works and shows the log message but isn't the whole point of asking for permissions (via dialogue) to turn on location (when it's off) if the user clicks "allow" ? Am I doing something wrong ? I should mention that I'm running the app on api23

is is the code in my Oncreate:

 mApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();

 mApiClient.connect();

    // Create the LocationRequest object
        mLocationRequest = LocationRequest.create()
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                .setInterval(10 * 1000)        // 10 seconds, in milliseconds
                .setFastestInterval(1 * 1000); // 1 second, in milliseconds

and this is my OnConnected method:

    public void onConnected(@Nullable Bundle bundle) {
            //start the service
//checking and asking for permission

            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(this,
                            new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                            MY_PERMISSION_ACCESS_FINE_LOCATION);
                }
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            Location location = LocationServices.FusedLocationApi.getLastLocation(mApiClient);

            if (location == null) {
                LocationServices.FusedLocationApi.requestLocationUpdates(mApiClient, mLocationRequest, this);

            } else {
                //If everything went fine lets get latitude and longitude
                currentLatitude = location.getLatitude();
                currentLongitude = location.getLongitude();
                Log.v("currentLatitude",currentLatitude + " WORKS " + currentLongitude + "");
            }

    }

解决方案

try this code:

private LocationCoord gps = null;
private static final int PERMISSION_REQUEST_CODE = 1;

In OnCreate():

//GPS Manage
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    boolean gps_enabled = false;
    boolean network_enabled = false;

    try {
        gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    } catch (Exception ex) {
    }

    try {
        network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    } catch (Exception ex) {
    }

    if (!gps_enabled && !network_enabled) {
        // notify user
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setMessage("Allow ImHere to access this device's location?");
        dialog.setPositiveButton("Allow", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                // TODO Auto-generated method stub
                Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(myIntent);
                //get gps
            }
        });
        dialog.setNegativeButton("Deny", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                // TODO Auto-generated method stub

            }
        });
        dialog.show();
    }

    gps = new LocationCoord(this);


@Override
protected void onStart() {
    super.onStart();

    // permission android 6.0
    if (!checkPermission()) {
        requestPermission();
    }

}


private boolean checkPermission(){
    int result = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
    if (result == PackageManager.PERMISSION_GRANTED) return true;
    else return false;
}

private void requestPermission(){
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQUEST_CODE);
}

You will need this permissions on the Manifest.xml:

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

You can get LoocationCord.java here: https://github.com/toomyy94/ImHere-Chatbot/blob/master/app/src/main/java/pt/ua/tomasr/imhere/modules/LocationCoord.java

这篇关于无法打开android api23上的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆