android6.0开启权限遇到的一些坑
一个小白,记录学习遇到的一些坑
最近在做android方面的实验,做到用LocationManager进行定位,发现单独在AndroidManifest.xml文件中
声明开启权限,不能够真正的开启想要的权限,说是安全问题,其实在日常中我们都很清楚的一个权限开启方式。
遇到问题,第一个想到的就是上网搜索,但是能解决问题的答案少的可怜,所以就记录下来,防止下次在遇到,不知所措。
废话不多说,其实很简单。在你报错的地方加上一个判断语句,判断当前的sdk版本是否大于23,大于就申请开启权限;否则直接跳过去执行你的代码
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
},2);
return;
}
}
下面是我要执行的代码,这段代码就是要申请开启权限的核心
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0,
locationListener);
原函数是
public static void requestPermissions(final @NonNull Activity activity,
final @NonNull String[] permissions, final @IntRange(from = 0) int requestCode) {
参数:
-
activity: 顾名思义,就是要申请权限的那个activity。
-
permissions: 要申请的权限名称,比如我申请的有
Manifest.permission.ACCESS_FINE_LOCATION,卫星定位要用到 Manifest.permission.ACCESS_COARSE_LOCATION.基站定位要用到
-
requestCode: 随便填个数,目前我了解这个参数在重写onRequestPermissionsResult这个方法用的到,重写这个方法可以了解到权限有没有开启成功。
可以通过重写该函数去显示权限开启成功还是失败
@Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { if (requestCode == 2) { for (int i = 0; i < permissions.length; i++) { if (grantResults[i] == PERMISSION_GRANTED) { Toast.makeText(this, "" + "权限" + permissions[i] + "申请成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "" + "权限" + permissions[i] + "申请失败", Toast.LENGTH_SHORT).show(); } } } }
下面我贴出我本次做的一个实验
- 布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/t1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/t2"/>
</LinearLayout>
- java代码
package com.example.locationmanage;
import android.Manifest;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Build;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import java.text.SimpleDateFormat;
import java.util.Date;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
public class MainActivity extends AppCompatActivity {
TextView info;
TextView location;
LocationManager locationManager;
LocationListener locationListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
info = findViewById(R.id.t1);
location = findViewById(R.id.t2);
//locationManger初始化
locationListener_init();
locationManager_init();
}
void locationManager_init() {
locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
locationListener_init();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PERMISSION_GRANTED) {
// TODO: Consider calling
// Activity#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for Activity#requestPermissions for more details.
ActivityCompat.requestPermissions(this,new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
},2);
return;
}
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0,
locationListener);
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
if (requestCode == 2) {
for (int i = 0; i < permissions.length; i++) {
if (grantResults[i] == PERMISSION_GRANTED) {
Toast.makeText(this, "" + "权限" + permissions[i] + "申请成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "" + "权限" + permissions[i] + "申请失败", Toast.LENGTH_SHORT).show();
}
}
}
}
void locationListener_init() {
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
SimpleDateFormat dateFormat = (SimpleDateFormat)
SimpleDateFormat.getTimeInstance(2);
String str = dateFormat.format(new Date(location.getTime()));
info.setText("时间:" + str + "\\n");
info.append("经度:" + location.getLongitude() + "\\n");
info.append("纬度:" + location.getLatitude() + "\\n");
info.append("海拔:" + location.getAltitude() + "\\n");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
if (status == LocationProvider.AVAILABLE)
info.setText("当前的gps可见的\\n");
else if (status == LocationProvider.OUT_OF_SERVICE)
info.setText("当前的gps服务区外\\n");
else if (status == LocationProvider.TEMPORARILY_UNAVAILABLE)
info.setText("当前的gps被暂停\\n");
}
@Override
public void onProviderEnabled(String provider) {
info.setText("当前的gps被启动\\n");
}
//被警用时触发
@Override
public void onProviderDisabled(String provider) {
info.setText("当前的gps被禁用\\n");
}
};
}
}
- 应用清单文件AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.locationmanage">
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
继续努力!~