有时开启新的app而内存不够时,其它app可能会被Out Of Memory Killer清除防止进程不被杀死的办法:
1.在AndroidManifest.xml文件中设置persistent属性为true
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:persistent="true">2.在前端运行程序
private void startForeground() {
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle(getResources().getString(R.string.app_name))
.setTicker(getResources().getString(R.string.app_name))
.setContentText("Running")
.setSmallIcon(R.drawable.unlock)
.setContentIntent(null)
.setOngoing(true)
.build();
startForeground(9999,notification);
}
本文介绍了两种防止Android应用因内存不足被系统终止的方法:一是通过在AndroidManifest.xml中设置应用为持久化;二是通过启动前台服务并创建一个始终运行的通知。

2601

被折叠的 条评论
为什么被折叠?



