一、網(wǎng)絡(luò)適配
從Android6.0開始google就建議使用https,不過你可以不鳥他繼續(xù)使用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"
...
>
二跺撼、權(quán)限適配
Android9之前在AndroidManifest.xml配置權(quán)限就可以了,但是Android 9開始只配置不行了讨彼,需要動態(tài)詢問用戶同不同意歉井,用戶不同意你配置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>(); //申請未得到授權(quán)的權(quán)限列表
private String[] permissionList = new String[]{ //申請的權(quán)限列表
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, //讀寫日歷的權(quán)限
Manifest.permission.WRITE_CALENDAR
};
//權(quán)限判斷和申請
public void checkPermission() {
unPermissionList.clear();//清空申請的沒有通過的權(quán)限
//逐個判斷是否還有未通過的權(quán)限
for (int i = 0; i < permissionList.length; i++) {
if (ContextCompat.checkSelfPermission(_context, permissionList[i]) !=
PackageManager.PERMISSION_GRANTED) {
unPermissionList.add(permissionList[i]);//添加還未授予的權(quán)限到unPermissionList中
}
}
//有權(quán)限沒有通過哈误,需要申請
if (unPermissionList.size() > 0) {
ActivityCompat.requestPermissions( _activity,permissionList, 100);
Log.i("TAG", "check 有權(quán)限未通過");
} else {
//權(quán)限已經(jīng)都通過了哩至,可以將程序繼續(xù)打開了
Log.i("TAG", "check 權(quán)限都已經(jīng)申請通過");
}
}
@Override
public void onRequestPermissionsResult( int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults ) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
2.使用方法
在BaseActivity.java中調(diào)用
@Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate(savedInstanceState);
//檢查權(quán)限
PermissionHelper permissionHelper = new PermissionHelper(this, BaseActivity.this);
permissionHelper.checkPermission();
}
三躏嚎、麥克風
你在配置麥克風權(quán)限以后可以正常使用麥克風,但是Android 9為了進一步保護用戶隱私規(guī)定在app休眠之后就不可以使用麥克風了菩貌,我在開發(fā)中遇到了這個問題卢佣,app退到后臺的50秒之后錄音文件沒有了內(nèi)容,下面提供解決方法
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");
//設(shè)置通知面板中顯示的視圖的信息仇参。
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,"顯示通知");
//發(fā)送通知
notificationManager.notify(NOTIFICATION,notification);
startForeground(R.string.notification_live_start,notification);
}else {
//設(shè)置通知面板中顯示的視圖的信息嘹叫。
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,"顯示通知");
//發(fā)送通知
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退到后臺休眠時不能調(diào)用麥克風的問題
Intent intent = new Intent(mContext,NotificationService.class);
mContext.startService(intent);
錄音結(jié)束時關(guān)閉
//適配Android 9 app退到后臺休眠時不能調(diào)用麥克風的問題
Intent intent = new Intent(mContext,NotificationService.class);
mContext.stopService(intent);
參考文章
mp3Recorder
https://github.com/GavinCT/AndroidMP3Recorder
Bad notification for startForeground錯誤解決
http://www.reibang.com/p/8baa62c5bfc2
android9.0 程序置入后臺或休眠麥克風不工作解決方法
https://blog.csdn.net/Crazy9599/article/details/89842280