除了Service的基本用法外,还有几个需要知道的知识点。
第一个就是前台服务
和Activity一样,Service在后台跑的时候是有优先级的,在内存紧张的情况下,级别低的会优先被杀死。而提高Service优先级,则可以降低内存杀死几率。
创建一个前台服务,不复杂。如下:
public class MyService extends Service {
public static final String TAG = "MyService";
private MyBinder mBinder = new MyBinder();
@Override
public void onCreate() {
super.onCreate();
Notification notification = new Notification(R.drawable.ic_launcher,
"xxx通知", System.currentTimeMillis());
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
notification.setLatestEventInfo(this, "xxx标题", "xxx内容",
pendingIntent);
startForeground(1, notification);
Log.d(TAG, "onCreate() executed");
}
.........
}
创建了一个Notification对象,然后调用了它的setLatestEventInfo()方法来为通知初始化布局和数据,并在这里设置了点击通知后就打开MainActivity。然后调用startForeground()方法就可以让MyService变成一个前台Service。
第二个则是IntentService
我们都知道Service和Activity是同级的,所以,要执行耗时任务,就得在Service里面开子线程来执行。但是有一种简单的方法来处理这个过程,那就是IntentService。
IntentService是继承于Service并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统Service一样,同时,当任务执行完后,IntentService会自动停止,而不需要我们去手动控制。另外,可以启动IntentService多次,而每一个耗时操作会以工作队列的方式在IntentService的onHandleIntent回调方法中执行,并且,每次只会执行一个工作线程,执行完第一个再执行第二个,以此类推。
IntentService帮我们省去了在Service中手动开线程的麻烦,第二,当操作完成时,我们不用手动停止Service。
IntentService可以启动多次,每启动一次,就会新建一个work thread,但IntentService的实例始终只有一个
public class IntentServiceDemo extends IntentService {
…
public IntentServiceDemo() {
//必须实现父类的构造方法
super(“IntentServiceDemo”);
}
@Override
public IBinder onBind(Intent intent) {
System.out.println("onBind");
return super.onBind(intent);
}
@Override
public void onCreate() {
System.out.println("onCreate");
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
System.out.println("onStart");
super.onStart(intent, startId);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void setIntentRedelivery(boolean enabled) {
super.setIntentRedelivery(enabled);
System.out.println("setIntentRedelivery");
}
@Override
protected void onHandleIntent(Intent intent) {
//Intent是从Activity发过来的,携带识别参数,根据参数不同执行不同的任务
String action = intent.getExtras().getString("type");
if (action.equals(TYPE_ONE)) {
System.out.println("type 1");
}else if (action.equals("TYPE_TWO)) {
System.out.println("type 2");
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void onDestroy() {
System.out.println("onDestroy");
super.onDestroy();
}
…
}
当然不要忘记了,他依然是一个Service,同样需要在清单文件中注册。
第三个,可能使用的场景不多,那就是屏保服务DreamService
DreamService的生命周期
1.onAttachedToWindow()
初始化设置,在这里可以调用 setContentView()
2.onDreamingStarted()
互动屏保已经启动,这里可以开始播放动画或者其他操作
3.onDreamingStopped()
在停止 onDreamingStarted() 里启动的东西
4.onDetachedFromWindow()
在这里回收前面调用的资源(比如 handlers 和 listeners)
另外,onCreate 和 onDestroy 也会被调用。但要复写上面的几个方法来执行初始化和销毁操作。
manifest 声明
为了能让系统调用,你的 DreamService 应该在 APP 的 manifest 中注册: