*

【android】サービスの実装

公開日: : 最終更新日:2013/03/21 android

ダウンロードなどActivityに依存したくない大きなバックグラウンド処理や常駐プロセスを作りたい場合、サービスを利用します。
サービスは大別すると

  • Intentから起動する実装
  • 特徴:実装がシンプル。
    用途:単純に長時間かかる処理をバックグラウンドで行いたい場合等に使用します。
    実例:ダウンロードやバックアップ処理など

  • Bindを利用した実装
  • 特徴:実装が少し複雑
    用途:常駐プロセスや、Activity内からサービスを操作したりサービスの処理結果を受け取りたい場合に使用します。
    実例:音楽プレイヤー

Bindを利用した例

編集・作成するファイル

  • IBindService.aidl
  • サービスのインターフェイスをaidlファイルで定義

  • BindService.java
  • aidlで定義したメソッドを実装

  • MyApp.manifest
  • サービスをマニフェストに登録

  • MyApp.java
  • Activityからサービスを起動する

IBindService.aidl

interface IBindService{
	void start();
	void echo(String txt);
	void finishService();
}

BindService.java

public class BindService extends Service{
	private final Context con = this;
	private final String TAG = BindService.class.getSimpleName();
	
	private final IBindService.Stub binder = new IBindService.Stub() {
		//IBindService.aidlで定義したメソッドを実装

		public void echo(final String message) throws RemoteException {
		}
		
	};

	@Override
	public void onCreate(){
		super.onCreate();
	}
	@Override
	public void onStart(Intent intent, int startId){
		super.onStart(intent, startId);
	}
	@Override
	public boolean onUnbind(Intent intent){
		super.onUnbind(intent);
		return false;
	}
	@Override
	public void onDestroy(){
		super.onDestroy();
	}
	@Override
	public IBinder onBind(Intent intent) {
		return binder;
	}
}

マニフェストファイル

        <service
            android:name=".BindService"
            android:process=":name"
            android:exported="false"
            >
            <intent-filter>
                <action android:name="com.full.package.name.IBindService" />
            </intent-filter>
        </service>

サービスを実行するActivity

//サービスと接続されたオブジェクト
IBindService ibService = null;
ServiceConnection serviceConnection;


private void bindService(){

	//接続処理を定義
	serviceConnection = new ServiceConnection(){
		public void onServiceConnected(ComponentName name, IBinder service) {
			ibService = IBindService.Stub.asInterface(service);
		}

		public void onServiceDisconnected(ComponentName name) {
		}
	};
	final Intent intent = new Intent(IBindService.class.getName());

	//サービスにバインドする。BIND_AUTO_CREATEを指定するとでサービスが起動していない場合は起動してからバインドするようになる。
	this.bindService(intent, serviceConnection, BIND_AUTO_CREATE);
}

	private void serviceMethod(){
		try {
			ibService.echo("text");
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	private void unBind(){
		Intent intent = new Intent(IBindService.class.getName());
		act.unbindService(serviceConnection);
		act.stopService(intent);
	}



関連記事

no image

【android】webviewでアプリ内にwebページを読み込む

webviewを使ってandroidアプリ内にwebページを読み込む定型文です。 //vie

記事を読む

no image

【android】アニメーション

Viewにアニメーションを付加する方法です。 目次 アニメーションの実行 de

記事を読む

no image

【android】任意のスレッドで処理を行う

非UIスレッドでUIを操作したい場合に、任意の処理をUIスレッド上で実行する。 目次

記事を読む

no image

【android】ネットワークの接続状況を確認する

コードから接続状況を確認。 ConnectivityManager co

記事を読む

no image

【android】標準の設定画面を作る

設定画面のテンプレートです。 検索一発クンのコードから抜粋しました。 チェックボックス、リス

記事を読む

no image

[android] モンキーテスト(Monkey Test)を実行する

最近テストの効率化に目覚めました。 モンキーテストは猿にアプリを渡してみてめちゃくちゃな操作をさせ

記事を読む

no image

インテント

基本 //任意のアクティビティの起動 Intent intent = new

記事を読む

no image

【android】 APIバージョンによる動作の違い

「最近使用したアプリケーション」からの起動 2.x系 -> onNewIntentが呼ばれる 4

記事を読む

no image

[android] JSONのパースにかかる時間

リストデータなんかを保存したいというのはよくある要件だと思います。 DBは面倒だしカラム毎に集計す

記事を読む

no image

【android】リスト項目のmatch_parentが効かない

下記のようなリスト項目用のレイアウトを用意して、リストの右端にチェックボックスを置くようにしたのです

記事を読む

Message

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

no image
知らないと損をする6つのライセンスまとめ

オープンソースやフリーウェア、フリー素材などが巷に溢れ、それらを利用す

no image
ガリレオ:ニュースブラウザをリリースしました。

概要 ガリレオはニュースを読んだり、検索する機能に特化したブラウザア

no image
【android】Activityとプロセスのライフサイクル

またもやライフサイクル周りでハマったのですが、Androidのライフサ

→もっと見る

PAGE TOP ↑