我正在开发一个使用意图服务备份某些文件的应用程序。该应用程序工作正常,不会崩溃。但是,当我在后台启动应用程序时,应用程序有时会在运行两天或有时仅在一个晚上关闭。
是因为该设备内存不足而我的应用无法正常关闭?
是否有任何意图,即我在我的应用程序中处理此情况,我将向用户说明上次进程停止的原因。
答案 0 :(得分:2)
是的。您的问题确实是由于设备内存不足。
当资源转低时,Android只会处理正在运行的服务,例如您的服务。
接下来会发生什么取决于你
声明为粘性
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
将上述内容添加到您的服务中会在部署资源后强制Android在重启后重启 再次上升。
如果正在运行的服务对您的系统至关重要,并且您不希望它在任何情况下自动被杀死 环境(提示:大部分时间这是不你想要的),你可以通过声明来做到这一点 它是前台服务
Intent intent = new Intent(this, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setTicker("ticker text").setContentTitle("title text").setContentText("content")
.setWhen(System.currentTimeMillis()).setAutoCancel(false)
.setOngoing(true).setPriority(Notification.PRIORITY_HIGH)
.setContentIntent(pendIntent);
Notification notif = builder.build();
notif.flags |= Notification.FLAG_NO_CLEAR;
startForeground(NOTIF_ID, notif);
最后注释:IntentService只生成一个执行所有请求的线程。
在许多情况下这很好用。但是,当谈到IO绑定执行时,通常会得到
使用多线程获得更好的性能
因此,只有在性能问题时,请考虑使用例如这个工作的多线程池:
ThreadFactory threadFactory = Executors.defaultThreadFactory();
ThreadPoolExecutor executorPool = new ThreadPoolExecutor(
MIN_NUM_THREADS, MAX_NUM_THREADS, 10, TimeUnit.SECONDS, ...);
...
executorPool.execute(new MyWorkerThread(params));
ThreadPoolExecutor构造函数接收的前两个参数设置最小和最大数 同时活动的线程。
答案 1 :(得分:0)
您可以使用它们来测量内存。
Gdx.app.getJavaHeap();
Gdx.app.getNativeHeap();
由于你的app在后台运行,也许你可以保存这些值供以后学习?