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

Android应用程序四大组件之Service(一)

2011-09-15 22:47 507 查看
What is a Service?
Service是一个应用程序组件,可以用来处理一些比较耗时的操作.

•A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of.

Service不是一个单独的进程.

• A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors).

Service不是一个线程.

启动方法

1.Content.startService()

此启动方式,不会与绑定者没有关系,即使程序退出,servcie仍然运行着.

2.Content.bindService()

bindService会与绑定者销毁,即程序退出,service就立即停止.

Service Lifecycle

两种Service启动方式不同,生命周期也不一样.

1.startService

启动服务(在要与绑定的activity上添加这行代码)
startService(new Intent(ServiceDemoActivity.this, MyService.class));

MyServcie.class

public class MyService extends Service{

@Override
public IBinder onBind(Intent intent) {
System.out.println("onBind()");
return myBinder;
}

@Override
public void onCreate() {
System.out.println("onCreate()");
super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("intent:"+intent+"(flags):"+flags+"startId:"+startId);
return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
System.out.println("onDestroy()");
super.onDestroy();
}
}

当我们启动Service时,响应的可以看到输出日志onCreate()->onStartCommand(); 多次startService并不会启动多个服务,onCreate()在创建时被调用,在Android系统1.x以前是并不是重写onStartCommand()而是onStart().onBind()在此启动方式并不会被调用.

//停住服务
(在要与绑定的activity上添加这行代码)
stopService(new Intent(ServiceDemoActivity.this, MyService.class));


当我们停止Service时,响应的可以看到输出日志onDestroy().

2.bindService

(在要与绑定的activity上添加这行代码)此service与application处于同一进程时情况.通过bindService启动服务.

bindService(new Intent(ServiceDemoActivity.this,MyService.class), serconn, BIND_AUTO_CREATE);
//需在onServiceConnected()从myservice中onBind()返回Ibinder对象,通过该对象取得相应的service实例
private ServiceConnection serconn = new ServiceConnection() {

@Override
public void onServiceDisconnected(ComponentName name) {
System.out.println("onServiceDisconnected");
}

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
System.out.println("onServiceConnected");

myService = ((MyBinder)service).getService();
mTextView.setText("get from Service time::"+myService.getSystemTime());

}
};



//在service类声明一个内部类继承Binder在该内部类中提供方法返回service实例.//在service类中声明这个内部的实例new MyBinder().以便onBind返回
public class MyService extends Service{

private MyBinder myBinder = new MyBinder();

@Override
public IBinder onBind(Intent intent) {
System.out.println("onBind()");
return myBinder;
}

@Override
public void onCreate() {
System.out.println("onCreate()");
super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("intent:"+intent+"(flags):"+flags+"startId:"+startId);
return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
System.out.println("onDestroy()");
super.onDestroy();
}

@Override
public boolean onUnbind(Intent intent) {
System.out.println("onUnbind()");
return super.onUnbind(intent);
}

@Override
public void onRebind(Intent intent) {
System.out.println("onRebind()");
super.onRebind(intent);
}

public class MyBinder extends Binder{
MyService getService(){
return MyService.this;
}
}

public String getSystemTime(){
return System.currentTimeMillis()+"";
}
}
当我启动服务时,可看见log输出onCreate()->onBind()->onServiceConnected
unbindService(serconn);
当我们停止服务时,可看见log输出onUnbind()->onDestroy()





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