1. 在同一个应用任何地方调用 startService() 方法就能启动 Service 了,然后系统会回调 Service 类的 onCreate() 以及 onStart() 方法。这样启动的 Service 会一直运行在后台,直到 Context.stopService() 或者 selfStop() 方法被调用。另外如果一个 Service 已经被启动,其他代码再试图调用 startService() 方法,是不会执行 onCreate() 的,但会重新执行一次 onStart() 。
2. 另外一种 bindService() 方法的意思是,把这个 Service 和调用 Service 的客户类绑起来,如果调用这个客户类被销毁,Service 也会被销毁。用这个方法的一个好处是,bindService() 方法执行后 Service 会回调上边提到的 onBind() 方发,你可以从这里返回一个实现了 IBind 接口的类,在客户端操作这个类就能和这个服务通信了,比如得到 Service 运行的状态或其他操作。如果 Service 还没有运行,使用这个方法启动 Service 就会 onCreate() 方法而不会调用 onStart()。
区别概况为:
startService() 的调用者与服务没有联系,即使调用者退出了,服务仍然运行,而bindService() 的调用者与服务绑在一起,调用者一旦退出了,服务也随即终止掉。www.2cto.com
这里用一个实例(使用startService()方法来启动)来讲解一下service的声明周期和使用方法:
首先编写一个类继承Service这个基类,重写里面的方法,然后在Activity中调用startService()和stopService()来启动和停止服务。
运行界面:
![]() 工程目录结构:
![]() ExampleService.java
[html]
package com.service.activity;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class ExampleService extends Service{
private static final String TAG = "ExampleService";
@Override
public void onCreate() {
Log.i(TAG, "ExampleService-onCreate");
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
Log.i(TAG, "ExampleService-onStart");
super.onStart(intent, startId);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//执行文件的下载或者播放等操作
Log.i(TAG, "ExampleService-onStartCommand");
/*
* 这里返回状态有三个值,分别是:
* 1、START_STICKY:当服务进程在运行时被杀死,系统将会把它置为started状态,但是不保存其传递的Intent对象,之后,系统会尝试重新创建服务;
* 2、START_NOT_STICKY:当服务进程在运行时被杀死,并且没有新的Intent对象传递过来的话,系统将会把它置为started状态,
* 但是系统不会重新创建服务,直到startService(Intent intent)方法再次被调用;
* 3、START_REDELIVER_INTENT:当服务进程在运行时被杀死,它将会在隔一段时间后自动创建,并且最后一个传递的Intent对象将会再次传递过来。
*/
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "ExampleService-onBind");
return null;
}
@Override
public void onDestroy() {
Log.i(TAG, "ExampleService-onDestroy");
super.onDestroy();
}
}
MainActivity.java
[html]
package com.service.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
private static final String TAG = "MainActivity"; //日志输出标志
private Button btnStartService;
private Button btnStopService;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnStartService = (Button)findViewById(R.id.btnStartService);
btnStopService = (Button)findViewById(R.id.btnStopService);
btnStartService.setOnClickListener(this);
btnStopService.setOnClickListener(this);
}
//点击事件处理监听器
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,ExampleService.class);
switch(v.getId()){
case R.id.btnStartService:
startService(intent);
break;
case R.id.btnStopService:
stopService(intent);
break;
default:
break;
}
}
}
最后在AndroidManifest.xml中对service进行声明,它跟Activity同一级,都写在Application标签里面:
<service android:name=".ExampleService"/>
创建完运行
在运行点击"启动service"之后(第一次启动service),我们可以查看LogCat控制台输出的日志如下:
![]() 这个时候我们点击"返回",Activity被干掉了,但是我们的服务仍然在运行,可以查看Setting-->Application-->Running Service,截图如下:
![]() 然后回到我们的Activity,再次点击启动Service,控制台输出日志如下:
![]() onCreate()方法没有被调用,说明它并没有重新被创建。
然后我们点击停止Service,输出日志如下:
![]() |
|