Services初步
有编程经验的朋友一听名字service就应该知道是提供的某种服务,而且是后台长时运行的,不提供ui,同样这也是android提供的又一个重要的应用组件。
service的两种表现形式
Started
应用组件(如activity)调用startService(),service被启动,一旦启动,通常,service在后台一直运行,不会和应用有任何交互,操作完成后,service应该自己停止。
Bound
应用组件(如activity)调用bindService(),service被绑定,一旦绑定,通常,service提供了允许组件和service有数据交互的客户端-服务器接口,只有组件绑定了service的情况下,service才会运行。
注意:service是运行在主线程的,如果有耗时操作,需要开线程来做。很方便的是android提供了一些类来帮助大家使用,比如下面介绍的IntentService(不能处理并发任务)
继承IntentService
需要实现的接口
构造函数:需要调用父类IntentService(string) 参数是队列名
onHandleIntent(Intent intent)
所做的工作:
1. 创建一个默认线程,此工作线程传递intent到onStartCommand,这样和主线程分离。
2. 创建一个工作队列,一次传递一个intent到onHandleIntent(),不用担心多线程并发问题。
3. 当所有请求被处理完毕,会自动停止service,不用手动调用stopSelf
4.提供默认onBind()实现
5.提供默认onStartCommand(),发送intent到工作队列,再到onHandleIntent()。
继承Service
-
public class HelloService extends Service {
-
private Looper mServiceLooper;
-
private ServiceHandler mServiceHandler;
-
-
// Handler that receives messages from the thread
-
private final class ServiceHandler extends Handler {
-
public ServiceHandler(Looper looper) {
-
super(looper);
-
}
-
@Override
-
public void handleMessage(Message msg) {
-
// Normally we would do some work here, like download a file.
-
// For our sample, we just sleep for 5 seconds.
-
long endTime = System.currentTimeMillis() + 5*1000;
-
while (System.currentTimeMillis() < endTime) {
-
synchronized (this) {
-
try {
-
wait(endTime - System.currentTimeMillis());
-
} catch (Exception e) {
-
}
-
}
-
}
-
// Stop the service using the startId, so that we don't stop
-
// the service in the middle of handling another job
-
stopSelf(msg.arg1);
-
}
-
}
-
-
@Override
-
public void onCreate() {
-
// Start up the thread running the service. Note that we create a
-
// separate thread because the service normally runs in the process's
-
// main thread, which we don't want to block. We also make it
-
// background priority so CPU-intensive work will not disrupt our UI.
-
HandlerThread thread = new HandlerThread("ServiceStartArguments",
-
Process.THREAD_PRIORITY_BACKGROUND);
-
thread.start();
-
-
// Get the HandlerThread's Looper and use it for our Handler
-
mServiceLooper = thread.getLooper();
-
mServiceHandler = new ServiceHandler(mServiceLooper);
-
}
-
-
@Override
-
public int onStartCommand(Intent intent, int flags, int startId) {
-
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
-
-
// For each start request, send a message to start a job and deliver the
-
// start ID so we know which request we're stopping when we finish the job
-
Message msg = mServiceHandler.obtainMessage();
-
msg.arg1 = startId;
-
mServiceHandler.sendMessage(msg);
-
-
// If we get killed, after returning from here, restart
-
return START_STICKY;
-
}
-
-
@Override
-
public IBinder onBind(Intent intent) {
-
// We don't provide binding, so return null
-
return null;
-
}
-
-
@Override
-
public void onDestroy() {
-
Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
-
}
-
}
start a service
Intent intent = new Intent(this, HelloService.class); startService(intent);
startService(intent);
阅读(1922) | 评论(0) | 转发(0) |