1. 用xml 合成图片
ic_launcher.xml 作用:将两个图片组合成一个图片,一个背景图,一个icon
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@mipmap/ic_launcher_background"/>
<foreground android:drawable="@mipmap/ic_launcher_foreground"/>
</adaptive-icon>
------生成了
2. Android中的app忽略电池优化
集成环信SDK时,进入页面,老是会弹出一个“要忽略电池优化吗?”的提示框,如果要去掉,把下列代码删除即可,代码如下:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String packageName = getPackageName();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (!pm.isIgnoringBatteryOptimizations(packageName)) {
try {
//some device doesn't has activity to handle this intent
//so add try catch
Intent intent = new Intent();
intent.setAction(android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
startActivity(intent);
} catch (Exception e) {
}
}
}
这个功能在manifest文件中配置了权限:
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
3. android9.0新特性:
(1)启动前台服务
Android在8.0限制了后台服务这些,启动后台服务需要设置通知栏,使服务变成前台服务。但是在9.0上,就会出现Permission Denial: startForeground requires android.permission.FOREGROUND_SERVICE
。
解决办法是在AndroidManifest
中添加
<!--android 9.0上使用前台服务,需要添加权限-->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
之后启动service的时候也需要做处理,否则会抛异常java.lang.IllegalStateException: Not allowed to start service Intent
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent);
} else {
context.startService(intent);
}
(2)Android 9.0强制使用https
Android 9.0强制使用https,会阻塞http请求,如果app使用的第三方sdk有http,将全部被阻塞。
出现UnknownServiceException: CLEARTEXT communication to localhost not permitted by network security policy
或者IOException java.io.IOException: Cleartext HTTP traffic to * not permitted
解决方法:
在AndroidManifest
文件的application
设置android:usesCleartextTraffic="true"
参考文献:
Android App兼容8.0和9.0
4. 拨打电话:
跳转到拨号界面:
public void callPhone(String phoneNum) {
Intent intent = new Intent(Intent.ACTION_DIAL);
Uri data = Uri.parse("tel:" + phoneNum);
intent.setData(data);
getContext().startActivity(intent);
}
直接拨打电话:
public void callAction(String telPhone) {
Intent intent = new Intent(Intent.ACTION_CALL);
Uri data = Uri.parse("tel:" + telPhone);
intent.setData(data);
getContext().startActivity(intent);
}