一、网络适配
从Android6.0开始google就建议使用https,不过你可以不鸟他继续使用http,但是从Android 9开始你就不得不鸟他了,因为http访问不了了。
1. 在res中新建xml文件夹
2.新建xml文件network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true"/>
<domain-config cleartextTrafficPermitted="true">
<!--IP01-->
<domain includeSubdomains="true">xx.xx.xx.xxx</domain>
<!--IP02-->
<domain includeSubdomains="true">xx.xx.xxx.xx</domain>
<!--IP03-->
<domain includeSubdomains="true">xx.xx.xx.xx</domain>
<!--bugly-->
<domain includeSubdomains="true">android.bugly.qq.com</domain>
</domain-config>
</network-security-config>
3.修改AndroidManifest.xml
<application
...
android:networkSecurityConfig="@xml/network_security_config"
...
>
二、权限适配
Android9之前在AndroidManifest.xml配置权限就可以了,但是Android 9开始只配置不行了,需要动态询问用户同不同意,用户不同意你配置10遍都没用。
1.新建java class工具类PermissionHelper
package com.example.jizhigang.crm_android_j.utils;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import com.example.jizhigang.crm_android_j.base.activity.BaseActivity;
import java.util.ArrayList;
import java.util.List;
public class PermissionHelper extends BaseActivity {
Context _context;
Activity _activity;
public PermissionHelper( Context _context, Activity _activity ) {
this._context = _context;
this._activity = _activity;
}
private List<String> unPermissionList = new ArrayList<String>(); //申请未得到授权的权限列表
private String[] permissionList = new String[]{ //申请的权限列表
Manifest.permission.INTERNET,
Manifest.permission.READ_CALL_LOG,
Manifest.permission.WRITE_CALL_LOG,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.CALL_PHONE,
Manifest.permission.CAMERA,
Manifest.permission.FOREGROUND_SERVICE,
Manifest.permission.READ_CALENDAR, //读写日历的权限
Manifest.permission.WRITE_CALENDAR
};
//权限判断和申请
public void checkPermission() {
unPermissionList.clear();//清空申请的没有通过的权限
//逐个判断是否还有未通过的权限
for (int i = 0; i < permissionList.length; i++) {
if (ContextCompat.checkSelfPermission(_context, permissionList[i]) !=
PackageManager.PERMISSION_GRANTED) {
unPermissionList.add(permissionList[i]);//添加还未授予的权限到unPermissionList中
}
}
//有权限没有通过,需要申请
if (unPermissionList.size() > 0) {
ActivityCompat.requestPermissions( _activity,permissionList, 100);
Log.i("TAG", "check 有权限未通过");
} else {
//权限已经都通过了,可以将程序继续打开了
Log.i("TAG", "check 权限都已经申请通过");
}
}
@Override
public void onRequestPermissionsResult( int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults ) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
2.使用方法
在BaseActivity.java中调用
@Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate(savedInstanceState);
//检查权限
PermissionHelper permissionHelper = new PermissionHelper(this, BaseActivity.this);
permissionHelper.checkPermission();
}
三、麦克风
你在配置麦克风权限以后可以正常使用麦克风,但是Android 9为了进一步保护用户隐私规定在app休眠之后就不可以使用麦克风了,我在开发中遇到了这个问题,app退到后台的50秒之后录音文件没有了内容,下面提供解决方法
1、新建service文件
public class NotificationService extends Service {
private static final String TAG = "NotificationService";
private NotificationManager notificationManager;
//通知的唯一标识号。
private int NOTIFICATION = R.string.notification_live_start;
@Override
public void onCreate() {
super.onCreate();
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
showNotification();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
private void showNotification(){
// PendingIntent如果用户选择此通知,则启动我们的活动
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,new Intent(this,NotificationService.class),0);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){
String channelId = createNotificationChannel("my_service","My Background Service");
//设置通知面板中显示的视图的信息。
Notification notification =new Notification.Builder(this,channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("正在通话")
.setContentTitle(getText(R.string.notification_live_start))
.setContentTitle("正在运行")
.setContentIntent(pendingIntent)
.build();
Log.d(TAG,"显示通知");
//发送通知
notificationManager.notify(NOTIFICATION,notification);
startForeground(R.string.notification_live_start,notification);
}else {
//设置通知面板中显示的视图的信息。
Notification notification =new Notification.Builder(this).setSmallIcon(R.mipmap.ic_launcher).setTicker("正在通话")
.setContentTitle(getText(R.string.notification_live_start))
.setContentTitle("正在运行")
.setContentIntent(pendingIntent)
.build();
Log.d(TAG,"显示通知");
//发送通知
notificationManager.notify(NOTIFICATION,notification);
startForeground(R.string.notification_live_start,notification);
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
private String createNotificationChannel( String channelId, String channelName){
NotificationChannel channel = new NotificationChannel(channelId,channelName,NotificationManager.IMPORTANCE_NONE);
channel.setLightColor(Color.BLUE);
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
nm.createNotificationChannel(channel);
return channelId;
}
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onDestroy() {
super.onDestroy();
notificationManager.cancel(NOTIFICATION);
}
}
2、使用方法
开始录音时启动
//适配Android 9 app退到后台休眠时不能调用麦克风的问题
Intent intent = new Intent(mContext,NotificationService.class);
mContext.startService(intent);
录音结束时关闭
//适配Android 9 app退到后台休眠时不能调用麦克风的问题
Intent intent = new Intent(mContext,NotificationService.class);
mContext.stopService(intent);
参考文章
mp3Recorder
https://github.com/GavinCT/AndroidMP3Recorder
Bad notification for startForeground错误解决
//www.greatytc.com/p/8baa62c5bfc2
android9.0 程序置入后台或休眠麦克风不工作解决方法
https://blog.csdn.net/Crazy9599/article/details/89842280