劉海屏適配
比如我們需要全屏顯示的時(shí)候,Google在api28中已經(jīng)做了處理。如下面代碼:
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//android p 劉海屏適配
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {//加版本判斷贪庙,28以下會(huì)報(bào)錯(cuò)
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.layoutInDisplayCutoutMode
= WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
getWindow().setAttributes(lp);
}
layoutInDisplayCutoutMode值說(shuō)明:
(1)LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT:默認(rèn)情況下,全屏窗口不會(huì)使用到挖孔區(qū)域拇舀,非全屏窗口可正常使用挖孔區(qū)域变抽。
(2)LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS:窗口聲明使用挖孔區(qū)域
(3)LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES:窗口聲明使用挖孔區(qū)域
(4)LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER:窗口聲明不使用挖孔區(qū)域
HTTP異常
將 compileSdkVersion 升級(jí)到 28 之后,如果在項(xiàng)目中用到了 Apache HTTP client 的相關(guān)類掠抬,就會(huì)拋出找不到這些類的錯(cuò)誤吼野。這是因?yàn)楣俜揭呀?jīng)在 Android P 的啟動(dòng)類加載器中將其移除,如果仍然需要使用 Apache HTTP client两波,可以在 Manifest 文件中加入:
<uses-library android:name="org.apache.http.legacy" android:required="false"/>
CLEARTEXT communication to life.115.com not permitted by network security policy
問題原因: Android P 限制了明文流量的網(wǎng)絡(luò)請(qǐng)求箫锤,非加密的流量請(qǐng)求都會(huì)被系統(tǒng)禁止掉
解決方案:
在資源文件新建xml目錄,新建文件network_security_config:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
清單文件配置:
android:networkSecurityConfig="@xml/network_security_config"
但還是建議都使用https進(jìn)行傳輸
限制非Activity場(chǎng)景啟動(dòng)Activity
從Android P開始雨女,只有當(dāng)Intent flag中指定了FLAG_ACTIVITY_NEW_TASK谚攒,才允許在非Activity場(chǎng)景啟動(dòng)Activity。如果不在Intent添加FLAG_ACTIVITY_NEW_TASK氛堕,將無(wú)法通過(guò)非Activity的Context啟動(dòng)一個(gè)Activity馏臭,并且會(huì)拋異常。例如在一個(gè)service中啟動(dòng)一個(gè)Activity:
@Override
public void onCreate() {
super.onCreate();
Intent intent = new Intent(this, MainActivity.class);
//不加 FLAG_ACTIVITY_NEW_TASK 將拋出異常
//intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
前臺(tái)服務(wù)需要添加權(quán)限
在安卓P版本之后讼稚,必須要授予FOREGROUND_SERVICE權(quán)限括儒,才能夠使用前臺(tái)服務(wù),否則會(huì)拋出異常锐想。例如:
@Override
public void onCreate() {
super.onCreate();
String channelID = "1";
String channelName = "channel_name";
NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
Intent intent = new Intent(this, ForegroundActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("前臺(tái)服務(wù)測(cè)試")
.setContentText("前臺(tái)服務(wù)需要增加 FOREGROUND_SERVICE 權(quán)限")
.setChannelId(channelID)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setContentIntent(pi)
.build();
startForeground(1, notification);
}
這是一個(gè)帶Notification的簡(jiǎn)單前臺(tái)服務(wù)帮寻, 如果我們沒有在AndroidManifest中注冊(cè)FOREGROUND_SERVICE權(quán)限,在Service啟動(dòng)的時(shí)候會(huì)拋出SecurityException異常赠摇。對(duì)此固逗,我們只需要在AndroidManifest添加對(duì)應(yīng)的權(quán)限即可浅蚪,這個(gè)權(quán)限是普通權(quán)限,不需要?jiǎng)討B(tài)申請(qǐng)烫罩。
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
限制靜態(tài)廣播的接收
升級(jí)安卓P之后惜傲,隱式廣播將會(huì)被全面禁止,在AndroidManifest中注冊(cè)的Receiver將不能夠生效贝攒,如果你的清單文件中有如下的監(jiān)聽器:
<receiver android:name="com.yanghaoyi.receiver.UpdateReceiver">
<intent-filter>
<action android:name="com.yanghaoyi.action.ACTION_UPDATE" />
</intent-filter>
</receiver>
你需要移除上面的代碼盗誊,并在應(yīng)用中進(jìn)行動(dòng)態(tài)注冊(cè),例如:
private void registerReceiver(){
myReceiver = new MyReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(TOAST_ACTION);
registerReceiver(myReceiver, intentFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(myReceiver);
}
非全屏透明Activity禁用設(shè)置orientation
非全屏透明頁(yè)面不允許設(shè)置方向隘弊,否則會(huì)拋Caused by: java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation異常哈踱,解決方案:android:windowIsTranslucent設(shè)置為false。