2011年7月14日木曜日

簡単なソースでサービスを調べる

Androidにはサービスという、Linux等でいうデーモンのような仕組みがある。
一口にサービスといっても、色々なコーディング方法がある。

まずは、何もしないサービスをActivityから呼び出して、調査しよう。


■何もしないサービスを作る
1)新規のAndroidプロジェクトを以下のように作成する。
「プロジェクト名」:ServiceTest0
「ビルドターゲット」:Android 1.6
「パッケージ名」:jp.co.example.servicetest0
「アクティビティ名」:ServiceTest0Activity

2)サービスクラスを作成する。
プロジェクトのsrcのパッケージ配下に、『MyService』クラスを作成
ソース内容は以下の通り。

基本的には、MyServiceクラスをServiceクラスの継承クラスとし、
onBindメソッドは、親のServiceクラスでabstractなので、強制的にオーバーライド。
onCreate、onDestroy、onStartは、オーバーライドは任意。
だが、何か処理をするためのサービスなので、通常はオーバーライド必須だろう。
各メソッドには、ログを埋め込んだ。

また、onStartメソッドには、スリープを5秒間入れた。
これは、onStartがどのスレッドで動くか調べるためだ。
結果、onStartは、Activityと同じmainスレッドで動作した。

つまりは、以下のコードにおけるサービスとは、
Activityと別プロセスどころか、別スレッドでもない。
(注:別プロセスで動くサービスもちゃんと用意されている)

package jp.co.example.servicetest0;

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 = this.getClass().getSimpleName();

 @Override
 public IBinder onBind(Intent arg0) {
  // TODO 自動生成されたメソッド・スタブ
  Log.d(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
  return null;
 }

 @Override
 public void onCreate() {
  // TODO 自動生成されたメソッド・スタブ
  super.onCreate();
  Log.d(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
 }

 @Override
 public void onDestroy() {
  // TODO 自動生成されたメソッド・スタブ
  super.onDestroy();
  Log.d(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());
 }

 @Override
 public void onStart(Intent intent, int startId) {
  // TODO 自動生成されたメソッド・スタブ
  super.onStart(intent, startId);
  Log.d(TAG, Thread.currentThread().getStackTrace()[2].getMethodName());

  Log.d(TAG, "sleep start");
  try {
   Thread.sleep(5000);
  } catch (InterruptedException e) {
   // TODO 自動生成された catch ブロック
   e.printStackTrace();
  }
  Log.d(TAG, "sleep end");
 }

}
3)画面にスタートボタン、ストップボタンを作る
res\layout\main.xmlに、ボタンを2つ作成。
ソースは以下のようになる。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Button android:text="Start" android:id="@+id/start_button" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    <Button android:text="Stop" android:id="@+id/stop_button" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>


4)サービスをスタート/ストップするアクティビティを作る
以下の処理を行っている。
スタートボタン押下で次を実行。
startService(new Intent(getBaseContext(), MyService.class));
ストップボタン押下で次を実行。
stopService(new Intent(getBaseContext(), MyService.class));

全体のコードは以下の通り。
package jp.co.example.servicetest0;

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 ServiceTest0Activity extends Activity {

 Button startButton;
 Button stopButton;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        findViews();

        startButton.setOnClickListener(new OnClickListener() {

   public void onClick(View v) {
    // TODO 自動生成されたメソッド・スタブ
    startService(new Intent(getBaseContext(), MyService.class));
   }
  });

        stopButton.setOnClickListener(new OnClickListener() {

     public void onClick(View v) {
      // TODO 自動生成されたメソッド・スタブ
      stopService(new Intent(getBaseContext(), MyService.class));
     }
    });
    }

    protected void findViews() {
     startButton = (Button)findViewById(R.id.start_button);
     stopButton = (Button)findViewById(R.id.stop_button);
    }
}

5)マニフェストでサービス許可する
AndroidManifest.xmlにタグにサービスクラス名を指定して、以下のようにサービスを許可する。

<application android:icon="@drawable/icon" android:label="@string/app_name">
 (略)
 <service android:name=".MyService"/>
</application>

0 件のコメント:

コメントを投稿