Started Service實例
再一次的重申:一個started service 是另一個組件靠調用startService()並傳遞一個Intent來啓動的,結果調用service的onStartCommend()並接受來自startService()傳遞過的Intent
- 有兩個你可以繼承的類去開啓一個started service
- Service類(加個類吧,免得引起誤會)
這個類對於所有的service是一個基本類,當你繼承這個類的時候,很有必要開啓一個新的線程去完成所有的service工作,依然是那個原因,這個service會使用你的主線程
- IntentService
這是service類的子類,繼承了這個類,那麼這個service將使用一個輔助線程去處理所有的開始請求,如果你不要求你的service同時去處理多種請求,那繼承這樣的服務將是一個好的選擇,你只需實現onHandleIntent()方法,這個方法接收每一個請求的intent
2.下面是started service的兩種方式的實例
繼承IntentService類
這個intentService要做以下幾件事:
創建一個默認的輔助線程執行將所有的intent發送給onStartCommand()
創建一個工作隊列每次只將一個intent發送給onHandleIntent(),所以你不必擔心多線程的問題
當所有的請求被處理完後,這個service將會停止,因此你也不必調用stopSelf()
提供一個默認的onBind(),返回的null值
提供一個默認的onStartCommand(),將發送intent給工作隊列,然後調用onHandleIntent()
不說了,下面是實例
這是MainActivity類
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage (View V)
{
System.out.println("你點擊了按鈕");
Intent intent = new Intent();
intent.setClass(MainActivity.this, HelloIntentService.class);
startService(intent);
}
}
這是mainAcivity類的xml配置文件
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="76dp"
android:layout_marginTop="115dp"
android:onClick="sendMessage"
android:text="開始" />
這是intentServiceexample類
public class HelloIntentService extends IntentService{
// 這個構造方法是必需的,調用父類的構造方法
public HelloIntentService() {
super("HelloIntentService");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
System.out.println("onStartCommand-------被調用");
return super.onStartCommand(intent, flags, startId);
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
// 我們可以在這裏處理一些工作,比如下載等等,這裏我們爲了看清楚他們的調用方法,將輸出提示信息
System.out.println("onHandleIntent --------被調用");
}
}
別忘了加
<service android:name="HelloIntentService"></service>
輸出結果可以用Logcat查看
如果你也一定要重寫其他的回調方法,想onCreate(),onStartComman()或onDestrory(),時一定要調用父類的實現
繼承Service類
如果你要求你的service執行一些多線程(而不是通過工作隊列處理開始請求),那麼你可以繼承Service類
爲了進行比較下面的例子,這個將和上面那個例子實現一樣的工作
這是MainActivity類
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View v)
{
System.out.println("你點擊了按鈕");
Intent intent = new Intent();
intent.setClass(MainActivity.this, HelloService.class);
startService(intent);
}
}
這MainActivity類的配置文件
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="76dp"
android:layout_marginTop="93dp"
android:text="點擊"
android:onClick="sendMessage"/>
這是HelloService類
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.Process;
import android.widget.Toast;
public class HelloService extends Service{
private Looper mServiceLooper;
private ServiceHandler mServiceHandler;
//接收來自線程的Message
private final class ServiceHandler extends Handler {
// ServiceHandler的構造方法
public ServiceHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
// 我們要做的工作可以在這裏寫
// 這裏我們只在logCat輸出信息
System.out.println("handleMessage------被調用");
// 使用 startId停止服務
// 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.
System.out.println("onCreate------被調用");
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) {
System.out.println("onStartCommand------被調用");
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 void onDestroy() {
System.out.println("onDestory------被調用");
Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
記得加
<service android:name="HelloService"></service>
結果是