android权限危险权限_新的Android LocationApi和权限

android权限危险权限

This is a follow on from a previous article I wrote about

这是我之前写过的一篇文章的后续文章

the old Android LocationListener. With Marshmallow (Android 6.0) Google is trying to deprecate the old LocationListener and enforce the use of the Google Location API. They have their reasons for this and as mere mortals we have to go along with them. 旧的Android LocationListener 。 Google尝试使用棉花糖(Android 6.0)弃用旧的LocationListener,并强制使用Google Location API。 他们有自己的理由,作为凡人,我们必须与他们同行。

Whilst re-writing an app to run on my newly upgraded-to-Marshmallow phone, I could not get the debugger with either Eclipse or Android Studio to work. Using my preferred IDE of Eclipse I even upgraded the Android Developer Tools (ADT) to Andmore but all I could get was

在重新编写要在新升级的棉花糖手机上运行的应用程序的同时,我无法使用Eclipse或Android Studio使调试器正常工作。 使用我偏爱的Eclipse IDE,我什至将Android Developer Tools(ADT)升级到Andmore,但我所能得到的只是

Can't bind to local 8601 for debugger
 ddmlib] An established connection was aborted by the software in your host machine
 java.io.IOException: An established connection was aborted by the software in your host machine

Googling, once more, proved little help.

再次谷歌搜索几乎没有帮助。

The new location activity looks like this, five new methods alongside the expected onCreate() method

新的位置活动如下所示,除了预期的onCreate()方法外还有五个新方法

public class My_location_activity extends Activity implements ConnectionCallbacks,
                                                          OnConnectionFailedListener
{

    @Override protected void onCreate(Bundle savedInstanceState)
    {
    }


    @Override protected void onStart()
    {
    }
    
    @Override protected void onStop()
    {
    }

    @Override public void onConnected(Bundle con_bundle)
    {
    }

    @Override public void onConnectionFailed(ConnectionResult provider)
    {
            
    }

    @Override public void onConnectionSuspended(int arg0)
    {        

    }        


}

To use the new location API you will need an API client in the onCreate() method, which is created using some ugly code:

要使用新的位置API,您需要在onCreate()方法中使用一个API客户端,该方法是使用一些难看的代码创建的:

@Override protected void onCreate(Bundle savedInstanceState)
{
 GoogleApiClient api_client = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();     
}

This client is then used with the onStart, onStop, onConnectionSuspended and onConnected methods. In onConnected the FusedLocationApi is used to populate the location from the API client:

然后将此客户端与onStart,onStop,onConnectionSuspended和onConnected方法一起使用。 在onConnected中,使用FusedLocationApi从API客户端填充位置:

@Override protected void onStart()
{
    super.onStart();
    api_client.connect();
}
    
@Override protected void onStop()
{
    super.onStop();
    api_client.disconnect();
}

@Override public void onConnectionSuspended(int arg0)
{        
    api_client.connect();        
}        


@Override public void onConnected(Bundle con_bundle)
{
    Location location = LocationServices.FusedLocationApi.getLastLocation(api_client);
}
        int prems = checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION);        
         
        if (perms != PackageManager.PERMISSION_GRANTED) 
        {    
            requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
                                            Manifest.permission.INTERNET, 
                                            Manifest.permission.ACCESS_NETWORK_STATE}, 1);                        
        }

This goes around a lot of the thinking behind checking the permissions, but I just want to do some debugging and this will allow me to do that. So if you have set a permission in the manifest, which location requires, there has to be a request to allow the user to grant it.

这涉及检查权限的很多想法,但是我只想进行一些调试,这将使我能够这样做。 因此,如果您在清单中设置了权限(需要该位置),则必须有一个请求以允许用户授予它。

翻译自: https://www.experts-exchange.com/articles/27099/The-new-Android-LocationApi-and-permissions.html

android权限危险权限

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值