服務有兩種:啓動式服務、綁定式服務。啓動式服務:被其他的組件(比如service)啓動,操作完了,調用某些方法停止。
生命週期:onCreate()-->onStartCommand()-->onDestory()
onStartCommand():當service被啓動的時候,使用startService()這個方法開啓,使用stopSelf()和stopService()來停止
onCreate():當service第一次被創建的時候執行
onDestory():當系統不再被使用或被銷燬
service一旦調用會一直執行,直到自己調用stopSelf()或者其他組件調用stopService()來停止它。
下面我們來看一下啓動式Service的生命週期中方法的調用順序。
整體思路:在xml文件中放入兩個Button控件,定義一個MyService類,繼承Service,在裏面重寫onCreate、onStartCommand、onDestroy三個方法,在onStartCommand方法中接收activity傳遞的字符串,並在三個方法中都輸出Log信息,用於顯示在執行Service的時候生命週期的方法調用情況。在activity中對兩個Button控件定義它的點擊事件,分別開啓Service並傳遞字符串變量和關閉Service。注意在清單文件AndroidManifest.xml中註冊Service。
activity_main.xml文件:
<RelativeLayout xmlns:android="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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="62dp"
android:text="啓動服務" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="44dp"
android:text="停止服務" />
</RelativeLayout>
MyService.java文件:
package com.example.android_service_life;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
private final String TAG="MyService";
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.i(TAG, "-->onCreate");
}
// 在onStartCommand方法中連接網絡獲取數據,關閉service服務
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.i(TAG, "-->onStartCommand"+" "+intent.getStringExtra("name"));
return super.onStartCommand(intent, flags, startId);
}
// 這個方法暫時不會執行
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
// 在onDestroy方法中釋放資源
@Override
public void onDestroy() {
// TODO Auto-generated method stub
Log.i(TAG, "-->onDestroy");
super.onDestroy();
}
}
MainActivity.java文件:
package com.example.android_service_life;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private Button start;
private Button stop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start=(Button)findViewById(R.id.button1);
stop=(Button)findViewById(R.id.button2);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,MyService.class);
intent.putExtra("name", "jack");
startService(intent);
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,MyService.class);
stopService(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}