开机启动
1、需要请求忽略电池优化
@RequiresApi(api = Build.VERSION_CODES.M)
public void ignoreBatteryOptimization() {
try{
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
boolean hasIgnored = powerManager.isIgnoringBatteryOptimizations(this.getPackageName());
if(!hasIgnored) {
Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:"+getPackageName()));
startActivity(intent);
}
}catch(Exception e){
//TODO :handle exception
}
}
2、在 设置里面,打开《应用后台启动、等信息 全部开启》
3、添加开机启动权限
请求网络服务
需要在工程路径res/xml路径存储 network_security_config.xml文件 配合下面代码段观看
下面是详细配置的XML
package="com.***.***.***">
android:allowBackup="true"
android:icon="@mipmap/ic_launcher_round"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:requestLegacyExternalStorage="true"
android:networkSecurityConfig="@xml/network_security_config"
android:theme="@style/Theme.**">