Android的 - 服务永葆 [英] Android - service keep alive

查看:84
本文介绍了Android的 - 服务永葆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个系统的一个问题。我已经运行的服务,这是不断检查的位置和计数的距离和时间,因为用户启动它。但20-25分钟,并与其他应用程序的服务许多相互作用后,被杀害。

我如何prevent呢?

我想添加第二个服务,将让我的活着。


解决方案

1,尽量减少你的服务的内存使用

2,让你的服务的前景,比如在服务的onCreate方法

  @覆盖
公共无效的onCreate()
{
     super.onCreate();     通知通知=新的通知(R.drawable.icon_app_small,它是gettext(R.string.app_name),System.currentTimeMillis的());
     意图notificationIntent =新意图(这一点,[yourService]的.class);
     的PendingIntent的PendingIntent = PendingIntent.getActivity(在此,0,notificationIntent,0);
     notification.setLatestEventInfo(这一点,[名字符串],[通知味精字符串]的PendingIntent);
     startForeground(Notification.FLAG_ONGOING_EVENT,通知);
}

have a problem with a system. I have running service, which is constantly checking the position and counting the distance and time since user start it. But after 20-25 minutes and many interactions with other applications service is being killed.

How I can prevent it?

I'm thinking to add second service which will keep my alive.

解决方案

1, minimize your service's memory usage

2, make you service foreground, for example in the service's onCreate method

@Override
public void onCreate()
{
     super.onCreate();

     Notification notification = new Notification(R.drawable.icon_app_small, getText(R.string.app_name),System.currentTimeMillis());
     Intent notificationIntent = new Intent(this, [yourService].class);
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
     notification.setLatestEventInfo(this, [name string], [notification msg string], pendingIntent);
     startForeground(Notification.FLAG_ONGOING_EVENT, notification);
}

这篇关于Android的 - 服务永葆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆