【android】サービスの実装
公開日:
:
最終更新日:2013/03/21
android
ダウンロードなどActivityに依存したくない大きなバックグラウンド処理や常駐プロセスを作りたい場合、サービスを利用します。
サービスは大別すると
- Intentから起動する実装
- Bindを利用した実装
特徴:実装がシンプル。
用途:単純に長時間かかる処理をバックグラウンドで行いたい場合等に使用します。
実例:ダウンロードやバックアップ処理など
特徴:実装が少し複雑
用途:常駐プロセスや、Activity内からサービスを操作したりサービスの処理結果を受け取りたい場合に使用します。
実例:音楽プレイヤー
Bindを利用した例
編集・作成するファイル
- IBindService.aidl
- BindService.java
- MyApp.manifest
- MyApp.java
サービスのインターフェイスをaidlファイルで定義
aidlで定義したメソッドを実装
サービスをマニフェストに登録
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); }
関連記事
-
【android】スリープモードに入らせない
Androidでは一定時間、操作をしないとスリープモードに入って待機状態になってしまいますが、動
-
【android】 Android4.1のserviceでdefaultPreferenceがおかしい
【現象】 ActivityでdefaultPreferencesで保存したデータがservice内
-
【android】リソースからいろいろ取得する
リソースで定義したいろいろをコード内で呼び出す方法です。 レイアウト //リソースからレ
-
【android】ハードウェアキー入力を検出する
Activityを継承したクラスで下記を記述します。 public boolean
-
【android】 APIバージョンによる動作の違い
「最近使用したアプリケーション」からの起動 2.x系 -> onNewIntentが呼ばれる 4
-
[android] ActivityとFragmentのライフサイクルいろいろ
Fragmentを使い始めてライフサイクル関係でハマることがあったので備忘録。 FragmentA
-
【android】アプリ内課金を実装する
アプリ内課金のサンプルコードを解析します。 参考URL 公式API Android
-
ダイアログを表示する
カスタムアラートダイアログ private void showCustomAlert(A
-
【android】 年月だけのDatePicker
final DatePicker datePicker = new DatePicker(sel
-
【android】IMEの表示/非表示
//IMEを閉じる InputMethodManager inputMethodManager