您的位置:首页 > 移动开发 > Android开发

Android进程级别与如何防止服务进程被回收

2015-10-10 15:57 393 查看
一、Android进程优先级

Android进程优先级具体如下,从高到低:

1、Foreground process 前台进程

下面几种情况属于前台进程:

(1)Activity正在与用户进程交互(Activity的onResume已经被调用)

(2)与正在和用户交互的Activity绑定的Service

(3)Service运行在前台——Service中调用了startForeground函数

(4)Service正在执行生命周期回调函数(onCreate,onStart,onDestory)

(5)BroadcastReceiver正在执行onReceive方法

2、Visible process 可视进程

下面几种情况属于可视进程:

(1)Activity没有运行在前台,但是用户仍然可见(它的onPause方法被调用),例如:当前台Activity启动了一个Dialog,这样Dialog运行在前台,Activity仍然可见,属于可视进程。

(2)与一个可视的Activity绑定的服务所在的进程

3、Service process 服务进程

运行服务的进程被startService()启动,并且没有进入上面1中(3)、(4)这两种情况。例如,音乐播放、网络下载数据

4、Background process 后台进程

当Activity不可见的时候,它的进程属于后台进程(Activity的onStop方法被调用)

5、Empty process 空进程

没有包含活动应用组件的进程为空进程,也就是进程的应用组件已经运行完毕。

下面我们进行一点总结:

1、假设当前进程只有Activity在运行:

当Activity运行onResume后,代表它获取到焦点,此时Activity运行的进程属于前台进程

当Activity运行onPause后,代表它失去了焦点,此时Activity运行的进程属于可视进程

当Activity运行onStop后,代表它不可见,此时Activity运行的进程属于后台进程

当Android运行了onDestory后,代表它运行完毕,此时Activity运行的进程属于空进程

2、服务进程是针对startService启动的服务所在的进程

3、绑定的服务的进程优先级跟绑定它的Activity密切相关

二、如何放在服务进程被回收

在音乐播放器或者网络下载的时候,我们经常我启动一个服务,把它们的操作放在一个服务中进行。从上面我们可以看到,服务进程的级别并不算高,当内存紧张的时候很可能被回收掉,下面我们讨论一下如何防止服务进程被回收。

其实从上面的讨论中,我们已经看到了一种很好的解决方法,上面提到在服务中使用startForeground函数可以使服务进程变成前台进程,这是一种很好的方法。

下面我们来看看这个方法:

public final void startForeground (int id, Notification notification)

它的第一个参数为notification的一个唯一标志符

它的第二个参数为一个Notification

当在服务中调用这个函数之后,服务进程会升级为前台进程,另外,它还会发送一个Notification,这个Notification处于状态栏”正在运行”的类别中,它是不能被手动移除,除非服务被停止或者它从前台移除。

如果希望从前台移除这个服务,只需要调用stopForeground(),一般情况我们只需要在onStartCommand里面调用 startForeground,然后再onDestroy里面调用stopForeground即可。

public class MusicService extends Service {
public MusicService() {
}

@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("MainActivity", "onStartCommand");
Notification notification = new Notification(R.drawable.ic_launcher, getText(R.string.hello_world),
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, getText(R.string.app_name),
getText(R.string.hello_world), pendingIntent);
startForeground(3, notification);

return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
super.onDestroy();
stopForeground(true);
}
}


下面提供另外几种防止服务进程被回收的方法:

1.把service写成系统服务,将不会被回收

在Manifest.xml文件中设置persistent属性为true,则可使该服务免受out-of-memory killer的影响。但是这种做法一定要谨慎,系统服务太多将严重影响系统的整体运行效率。

2.提高service的优先级

设置android:priority=”1000”

<service android:name="com.example.helloandroid.weatherforecast.service.UpdateWidgetService" android:exported="false" >
<!-- 为防止Service被系统回收,可以通过提高优先级解决,1000是最高优先级,数字越小,优先级越低 -->
<intent-filter android:priority="1000"></intent-filter>
</service>


3.利用ANDROID的系统广播检查Service的运行状态,如果被杀掉,就再起来

利用的系统广播是Intent.ACTION_TIME_TICK,这个广播每分钟发送一次,我们可以每分钟检查一次Service的运行状态,如果已经被结束了,就重新启动Service。

参考文章:

使用startForeground让android服务前台运行

Processes and Threads

Android Service被系统回收的解决方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: