Android10开机启动和网络 参数设置 华为EMUI11
程序员文章站
2022-05-01 19:41:53
开机启动1、需要请求忽略电池优化2、在 设置里面,打开《应用后台启动、等信息》3、添加开机启动权限请求网络取消在RES/XML路径存储network_security_config.xml文件 配合下面代码段观看 ....
开机启动
1、需要请求忽略电池优化
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
@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、添加开机启动权限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".ppBroadcastReceiver">
<intent-filter android:priority="2147483647">
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
请求网络服务
需要在工程路径res/xml路径存储 network_security_config.xml 文件 配合下面代码段观看
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
下面是详细配置的XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.***.***.***">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
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.**">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".ppBroadcastReceiver">
<intent-filter android:priority="2147483647">
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".ppService">
</service>
</application>
</manifest>
本文地址:https://blog.csdn.net/weixin_37615774/article/details/112642092