通话要求许可权可能会被用户拒绝 [英] Call requires permissions that may be rejected by user

查看:171
本文介绍了通话要求许可权可能会被用户拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个每五分钟发送一次用户位置更新的应用程序.我想我的代码可以正常工作,但是我收到关于应用程序正在使用的权限的错误消息.我很确定我已经在清单文件中添加了权限.有人可以告诉我怎么了吗?这是我的代码.

I am trying to make an application that sends location updates of a user after every five minutes. I suppose my code is working just fine but i get an error regarding the permissions that are being used by the application. I am pretty sure that i have added the permissions in the manifest file. Can someone tell me what's wrong? Here is my code.

MainActivity.java

LocationManager locationManager ;
String provider;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Getting LocationManager object
    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

    // Creating an empty criteria object
    Criteria criteria = new Criteria();

    // Getting the name of the provider that meets the criteria
    provider = locationManager.getBestProvider(criteria, false);

    if(provider!=null && !provider.equals("")){

        // Get the location from the given provider
        Location location = locationManager.getLastKnownLocation(provider);
        locationManager.requestLocationUpdates(provider,5*60*1000,0,this);

        if(location!=null)
            onLocationChanged(location);
        else
            Toast.makeText(getBaseContext(), "Location can't be retrieved", Toast.LENGTH_SHORT).show();

    }else{
        Toast.makeText(getBaseContext(), "No Provider Found", Toast.LENGTH_SHORT).show();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onLocationChanged(Location location) {
    // Getting reference to TextView tv_longitude
    TextView tvLongitude = (TextView)findViewById(R.id.tv_longitude);

    // Getting reference to TextView tv_latitude
    TextView tvLatitude = (TextView)findViewById(R.id.tv_latitude);

    // Setting Current Longitude
    tvLongitude.setText("Longitude:" + location.getLongitude());

    // Setting Current Latitude
    tvLatitude.setText("Latitude:" + location.getLatitude() );
}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub
}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub
}

}

我收到一个错误消息,因为通话需要许可,而这些行中的用户可能会拒绝-

I am getting an error as Call requires permission which may be rejected by user in these lines-

Location location = locationManager.getLastKnownLocation(provider);
        locationManager.requestLocationUpdates(provider,5*60*1000,0,this);

我的 AndroidManifest 就是这样

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>

<application
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

推荐答案

您使用哪个SDK? 如果您使用棉花糖,则需要检查该用户是否已授予每个位置调用的权限.

Which SDK do you use? If you use Marshmallow, then you need to check that the user has granted permission for every location call.

在这里.

您应该执行以下操作:

  if (ContextCompat.checkSelfPermission( this,android.Manifest.permission.ACCESS_COARSE_LOCATION ) != PackageManager.PERMISSION_GRANTED )
{
    ActivityCompat.requestPermissions(
        this,
        new String [] { android.Manifest.permission.ACCESS_COARSE_LOCATION },
        LocationService.MY_PERMISSION_ACCESS_COURSE_LOCATION
    );
}

请求权限(如果您还没有的话).

request permission if you dont have it already.

查看上面的链接以获取更多信息.

check the link above for more info.

这篇关于通话要求许可权可能会被用户拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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