android - 调用需要可能被用户拒绝的权限

标签 android runtime-error locationmanager

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

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
}

}

我收到一个错误,因为 调用需要权限,这可能会被用户拒绝在这些行中 -

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? 如果您使用 Marshmallow,则需要检查用户是否已授予每次定位调用的权限。

看看Here.

你应该这样做:

  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
    );
}

如果您还没有权限,请请求权限。

查看上面的链接了解更多信息。

关于android - 调用需要可能被用户拒绝的权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33327984/

相关文章:

android - ListView 之间的按钮消失

android - 应用从错误的 Activity 中重启

c++ unordered_map vector 下标超出范围

ios - 应用程序进入后台后每 n 分钟获取一次用户位置

java - Lollipop 握手失败

Android 谷歌地图去当前位置没有动画

python - Django:没有名为 'app' 的模块

R - SVM 训练后的奇怪错误/警告 (e1071)

android - MyLocationOverlay 与 LocationManager 和后台服务

android - 运行时错误--java.lang.IllegalArgumentException : provider=gps