Android中启动Service的两种方式:
startService()方式启动的service和启动它的activity的生命周期无关。
Android Service的生命周期包括onCreate(), onStart(), onDestroy()。
startService()方式启动Service,系统会回调Service的onCreate()、onStart()。这种方式启动的Service会一直运行,直到再次调用stopService()或者Service自身停止。如果Service已经运行,再次调用startService()方法,是不会再执行onCreate()的,只会执行onStart()。
下面用一个示例代码来验证采用startService的启动方式时,Activity和Service的在生命周期方面的关系:
(1)Eclipse工程截图:
(3)代码:
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "MainActivity --onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnStart = (Button) findViewById(R.id.start_service);
Button btnStop = (Button) findViewById(R.id.stop_service);
Button btnFinish = (Button) findViewById(R.id.finish_activity);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG, "MainActivity --btnStart onClick");
startService();
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG, "MainActivity --btnStop onClick");
stopService();
}
});
btnFinish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG, "MainActivity --btnFinish onClick");
finish();
}
});
}
@Override
protected void onStart() {
Log.i(TAG, "MainActivity --onStart");
super.onStart();
}
@Override
protected void onResume() {
Log.i(TAG, "MainActivity --onResume");
super.onResume();
}
@Override
protected void onPause() {
Log.i(TAG, "MainActivity --onPause");
super.onPause();
}
@Override
protected void onStop() {
Log.i(TAG, "MainActivity --onStop");
super.onStop();
}
@Override
protected void onDestroy() {
Log.i(TAG, "MainActivity --onDestroy");
super.onDestroy();
}
private void startService(){
Log.i(TAG, "MainActivity --startService");
Intent intent = new Intent(MainActivity.this, Service.class);
startService(intent);
}
private void stopService(){
Log.i(TAG, "MainActivity --stopService");
Intent intent = new Intent(MainActivity.this, Service.class);
stopService(intent);
}
}
public class MainService extends Service {
private static final String TAG = "MainService";
private MyBinder mBinder = new MyBinder();
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "MainService --onBind()");
return mBinder;
}
public class MyBinder extends Binder{
public MainService getService(){
return MainService.this;
}
}
@Override
public void onCreate() {
Log.i(TAG, "MainService --onCreate()");
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
Log.i(TAG, "MainService --onStart()");
super.onStart(intent, startId);
}
@Override
public void onDestroy() {
Log.i(TAG, "MainService --onDestroy()");
super.onDestroy();
}
@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG, "MainService --onUnbind()");
return super.onUnbind(intent);
}
}
"http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dadi.service_by_start"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="10" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.dadi.service_by_start.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
intent-filter>
activity>
<service android:name="com.dadi.service_by_start.MainService">service>
application>
manifest>