Android开发之如何保证Service不被杀掉(前台服务)

序言

最近项目要实现这样一个效果:运行后,要有一个service始终保持在后台运行,不管用户作出什么操作,都要保证service不被kill。参考了现今各种定制版的系统和安全厂商牛虻软件,如何能保证自己的Service不被杀死呢?其实除了常规的手段,我们可以参考一下微信和360,设置-程序-正在运行,可以看到微信是同时开启了两个进程和服务:

Android开发之如何保证Service不被杀掉(前台服务)

Service简介

Service是在一段不定的时间运行在后台,不和用户交互应用组件。每个Service必须在manifest中 通过<service>来声明。可以通过contect.startservice和contect.bindserverice来启动。和其他的应用组件一样,运行在进程的主线程中。这就是说如果service需要很多耗时或者阻塞的操作,需要在其子线程中实现(或者用系统提供的IntentService,它继承了Service,它处理数据是用自身新开的线程)。【当然你也可以在新的线程中startService,这样Service就不是在MainThread了

本地服务 Local Service 用于应用程序内部

它可以启动并运行,直至有人停止了它或它自己停止。在这种方式下,它以调用Context.startService()启动,而以调用Context.stopService()结束。它可以调用Service.stopSelf() 或 Service.stopSelfResult()来自己停止。不论调用了多少次startService()方法,你只需要调用一次stopService()来停止服务。

【用于实现应用程序自己的一些耗时任务,比如查询升级信息,并不占用应用程序比如Activity所属线程,而是单开线程后台执行,这样用户体验比较好】

远程服务 Remote Service 用于Android系统内部的应用程序之间

它可以通过自己定义并暴露出来的接口进行程序操作。客户端建立一个到服务对象的连接,并通过那个连接来调用服务。连接以调用Context.bindService()方法建立,以调用 Context.unbindService()关闭。多个客户端可以绑定至同一个服务。如果服务此时还没有加载,bindService()会先加载它。

【可被其他应用程序复用,比如天气预报服务,其他应用程序不需要再写这样的服务,调用已有的即可】

1,Service的生命周期

Android开发之如何保证Service不被杀掉(前台服务)

2,Service运行方式

以startService()启动服务,系统将通过传入的Intent在底层搜索相关符合Intent里面信息的service。如果服务没有启动则先运行onCreate,然后运行onStartCommand (可在里面处理启动时传过来的Intent和其他参数),直到明显调用stopService或者stopSelf才将停止Service。无论运行startService多少次,只要调用一次stopService或者stopSelf,Service都会停止。使用stopSelf(int)方法可以保证在处理好intent后再停止。onStartCommand ,在2.0后被引入用于service的启动函数,2.0之前为public void onStart(Intent intent, int startId) 。

以bindService()方法启用服务,调用者与服务绑定在了一起,调用者一旦退出,服务也就终止。onBind()只有采用Context.bindService()方法启动服务时才会回调该方法。该方法在调用者与服务绑定时被调用,当调用者与服务已经绑定,多次调用Context.bindService()方法并不会导致该方法被多次调用。采用Context.bindService()方法启动服务时只能调用onUnbind()方法解除调用者与服务解除,服务结束时会调用onDestroy()方法。

3,拥有service的进程具有较高的优先级

官方文档告诉我们,Android系统会尽量保持拥有service的进程运行,只要在该service已经被启动(start)或者客户端连接(bindService)到它。当内存不足时,需要保持,拥有service的进程具有较高的优先级。

1. 如果service正在调用onCreate,onStartCommand或者onDestory方法,那么用于当前service的进程则变为前台进程以避免被killed。
2. 如果当前service已经被启动(start),拥有它的进程则比那些用户可见的进程优先级低一些,但是比那些不可见的进程更重要,这就意味着service一般不会被killed.
3. 如果客户端已经连接到service (bindService),那么拥有Service的进程则拥有最高的优先级,可以认为service是可见的。
4. 如果service可以使用startForeground(int, Notification)方法来将service设置为前台状态,那么系统就认为是对用户可见的,并不会在内存不足时killed。
5. 如果有其他的应用组件作为Service,Activity等运行在相同的进程中,那么将会增加该进程的重要性。

实现前台:

 Service如果要防止尽可能不被系统杀掉,需要设置为在前台运行。

1.MainActivity.java

 package com.example.helloteacher;

 import com.example.teacherService.HelloteacherService;
import com.example.teacherService.HelloteacherService2; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView; public class MainActivity extends Activity {
private TextView fuction;
private Button start;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
init();
//创建进程
inProcess();
}
///初始化控件函数
private void init() {
start=(Button)findViewById(R.id.button1);
fuction=(TextView)findViewById(R.id.tv_bottom_funtion);
start.setOnClickListener(new StartOnClickListener());
fuction.setOnClickListener(new FuctionOnClickListener());
}
//功能按钮监听实现函数
private final class FuctionOnClickListener implements View.OnClickListener{ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, FuctionActivity.class);
startActivity(intent);
}
}
//进程函数
private void inProcess(){
Intent intent =new Intent(MainActivity.this, HelloteacherService.class);
startService(intent);
Intent intent2 =new Intent(MainActivity.this, HelloteacherService2.class);
startService(intent2);
}
private final class StartOnClickListener implements View.OnClickListener{ @Override
public void onClick(View v) { }
} }

2.HelloteacherService.java

 package com.example.teacherService;

 import com.example.Receiver.Alarmreceiver;
import com.example.helloteacher.R; import android.app.AlarmManager;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log; public class HelloteacherService extends Service {
HelloteacherService2 hs2;
private String TAG="HelloteacherService";
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "-->>onCreate");
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "-->>onStartCommand-->>"+startId);
flags = START_STICKY; //启用前台服务,主要是startForeground()
Notification notification = new Notification(R.drawable.ic_launcher, "用电脑时间过长了!白痴!"
, System.currentTimeMillis());
notification.setLatestEventInfo(this, "快去休息!!!",
"一定保护眼睛,不然遗传给孩子,老婆跟别人跑啊。", null);
//设置通知默认效果
notification.flags = Notification.FLAG_SHOW_LIGHTS;
startForeground(1, notification); AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
//读者可以修改此处的Minutes从而改变提醒间隔时间
//此处是设置每隔55分钟启动一次
//这是55分钟的毫秒数
int Minutes = 55 * 60 * 1000;
//SystemClock.elapsedRealtime()表示1970年1月1日0点至今所经历的时间
long triggerAtTime = SystemClock.elapsedRealtime() + Minutes;
//此处设置开启AlarmReceiver这个Service
Intent i = new Intent(this, Alarmreceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
//ELAPSED_REALTIME_WAKEUP表示让定时任务的出发时间从系统开机算起,并且会唤醒CPU。
manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi); return super.onStartCommand(intent, flags, startId);
}
//监听服务2实现函数 @Override
public IBinder onBind(Intent arg0) {
Log.i(TAG, "-->>onBind");
// TODO Auto-generated method stub
return null;
}
@Override
public void onDestroy() {
Log.i(TAG, "-->>onDestroy");
super.onDestroy(); }
}

3.BootReceiver.java

 package com.example.Receiver;

 import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock; public class BootReceiver extends BroadcastReceiver { /*要接收的intent源*/
static final String ACTION = "android.intent.action.BOOT_COMPLETED"; public void onReceive(Context context, Intent mintent)
{
if (Intent.ACTION_BOOT_COMPLETED.equals(mintent.getAction())) {
// 启动完成
Intent intent = new Intent(context, Alarmreceiver.class);
intent.setAction("arui.alarm.action");
PendingIntent sender = PendingIntent.getBroadcast(context, 0,
intent, 0);
long firstime = SystemClock.elapsedRealtime();
AlarmManager am = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE); // 10秒一个周期,不停的发送广播
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstime,
10 * 1000, sender);
}
} }

4.Alarmreceiver.java

 package com.example.Receiver;

 import com.example.teacherService.HelloteacherService;

 import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent; public class Alarmreceiver extends BroadcastReceiver { @Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals("arui.alarm.action")) {
Intent i = new Intent();
i.setClass(context, HelloteacherService.class);
// 启动service
// 多次调用startService并不会启动多个service 而是会多次调用onStart
context.startService(i);
}
} }

5.配置文件

AndroidManifest.xml

下面是添加在配置文件中的部分代码:

  <activity android:name="com.example.helloteacher.FuctionActivity" />
<service android:name="com.example.teacherService.HelloteacherService" android:process=":HelloteacherService"/>
<service android:name="com.example.teacherService.HelloteacherService2"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<receiver android:name="com.example.Receiver.BootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name="com.example.Receiver.Alarmreceiver" >
<intent-filter>
<action android:name="arui.alarm.action" />
</intent-filter>
</receiver>

通过以上的配置,就可以实现Service的前台运行!下面附上效果图:

Android开发之如何保证Service不被杀掉(前台服务)

上一篇:Android 开发之如何保证Service不被杀掉(broadcast+system/app)


下一篇:Android开发之如何保证Service不被杀掉(broadcast+system/app