由于应用中会集成
Android权限分为两种,normal和dangerous。normal为普通权限,不需要程序动态申请,在Manifest中声明就可以。dangerous为敏感权限,需要程序动态申请。
public static final String QUERY_ALL_PACKAGES
Allows query of any normal app on the device, regardless of manifest declarations.
Protection level: normal
Constant Value: "android.permission.QUERY_ALL_PACKAGES"
public static final String POST_NOTIFICATIONS
Allows an app to post notifications
Protection level: dangerous
Constant Value: "android.permission.POST_NOTIFICATIONS"
当应用请求 ACCESS_COARSE_LOCATION 权限或 ACCESS_FINE_LOCATION 权限时(如以下代码段所示),就是在声明需要获取前台位置信息:
<manifest ... >
<!-- Always include this permission -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- Include only if your app benefits from precise location access. -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>
权限分为清单文件配置权限以及运行时权限。
- android 6.0之前,所有的权限在Manifest中声明就可以了。
- android 6.0之后,推出了运行时权限。首先,需要应用在Manifest中配置权限,其次,需要动态获取权限,弹出系统弹窗后,用户点击同意才会获取到权限。
- 检测应用是否已经拥有此权限。
- 如果应用已经具备此权限,不做特殊处理,如果没有拥有权限,则弹出系统弹窗申请权限。(用户拒绝两次系统弹窗之后便不会再弹窗了。)
- 根据用户对系统弹窗选择同意或者拒绝进行操作。
网络权限
<uses-permission android:name="android.permission.INTERNET" />
查看网络状态权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
有了查看网络状态权限之后,就可以获取网络信息了。
NetworkInfo activeNetworkInfo = systemService.getActiveNetworkInfo();
需要ACCESS_NETWORK_STATE权限。
如果使用http进行信息的传递,需要在manifest的application中进行以下说明:
android:usesCleartextTraffic="true"
<application
android:usesCleartextTraffic="true"
android:name=".JJApmApplication"
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/Theme.JJAPMSDK">
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == requestStorePermissionCode) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// 用户授权了权限
// 执行相应操作
ToastUtil.showToast(activity,"用户授予了存储权限");
} else {
// 用户拒绝了权限
// 执行相应操作
ToastUtil.showToast(activity,"用户拒绝授予存储权限");
}
}
}
主程序如下:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private String TAG = "JJStudy.callBack";
private Button goAddressBook;
private Button goPhotoLibrary;
private String[] permission_SMS = {
Manifest.permission.SEND_SMS,
Manifest.permission.READ_SMS
};
private int permission_SMS_code = 10011;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
goAddressBook.setOnClickListener(this);
goPhotoLibrary.setOnClickListener(this);
}
private void initView() {
switchBtn = (Button) findViewById(R.id.switch_btn);
goAddressBook = (Button) findViewById(R.id.goAddressBook);
goPhotoLibrary = (Button) findViewById(R.id.goPhotoLibrary);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.goAddressBook:
if (PermissionUtil.checkPermission(this,permission_SMS)){
// 已授权
Log.i(TAG,"已经收到授权!!!!");
}else {
// 未授权
PermissionUtil.requestPermission(this,permission_SMS,permission_SMS_code);
}
break;
case R.id.goPhotoLibrary:
break;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == permission_SMS_code){
if (PermissionUtil.checkGranted(grantResults)){
// 授权成功
Log.i(TAG,"granted after system pop...");
}else {
// 用户未授权
Log.i(TAG,"not granted after system pop...");
}
}
}
}
工具类如下,:
package com.hp.interfacecallback;
import android.app.Activity;
import android.content.pm.PackageManager;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
public class PermissionUtil {
public static boolean checkPermission(Activity activity, String permission) {
return checkPermission(activity, new String[]{permission});
}
public static boolean checkPermission(Activity activity, String[] permissions) {
boolean result = true;
int permissionGranted = PackageManager.PERMISSION_GRANTED;
for (int i = 0; i < permissions.length; i++) {
permissionGranted = ContextCompat.checkSelfPermission(activity, permissions[i]);
/**
* 检测所有申请的权限是否都已经授权,没有跳出循环
*/
if (permissionGranted == PackageManager.PERMISSION_DENIED) {
result = false;
break;
}
}
return result;
}
public static void requestPermission(Activity activity, String[] permissions,int requestCode){
ActivityCompat.requestPermissions(activity,permissions,requestCode);
}
public static boolean checkGranted(int[] grantResults) {
if (null == grantResults || grantResults.length == 0){
return true;
}
boolean granted = true;
for (int i = 0; i < grantResults.length; i++) {
if (grantResults[i] == PackageManager.PERMISSION_DENIED){
granted = false;
break;
}
}
return granted;
}
}