ServiceにActivityをBindしたのですが、onServiceConnectedメソッドが呼ばれません

8,128 views
Skip to first unread message

S. Kimura

unread,
Nov 12, 2010, 6:58:16 PM11/12/10
to 日本Androidの会
はじめまして。

現在、練習用としてTimerのアプリケーションを作っているのですが、
ServiceにActivityをbindしたのですが、onServiceConnectedが呼ばれず、
Activity側からServiceを操作できない状態になってしまっています。
#Serviceクラス側のonBindも呼ばれない状態です。

現在の状況としては下記の通りです。

・サービスクラスには下記のようにBinderを定義し、サービスを取得可能にしている
public class TimerAlarmService extends Service
{
public class TimerAlarmBinder extends Binder
{
TimerAlarmService getService()
{
return TimerAlarmService.this;
}
}


・サービスクラスは下記のようにActivityから複数種のキーを指定して
 ブロードキャスト用のイベントを起動可能としている。

/**
* 固定遅延後に開始、繰り返し実行するタスクを追加
* generateTimer()実行後に使用。
*
* @param delay 遅延時間
* @param period 実行間隔
* @param broadCastKey 実行時のイベントキー
*/
public void schedule(long delay, long period, final String
broadCastKey)
{
TimerTask timerTask = new TimerTask() {
public void run()
{
sendBroadcast(new Intent(broadCastKey));
}
};
timer_.schedule(timerTask, delay, period);
}


・ActivityではServiceConnectionをフィールド変数として定義
/** サービスクラスへの接続フィールド */
ServiceConnection connection_

= new ServiceConnection() {
@Override
public void onServiceConnected(
ComponentName className,
IBinder service)
{
timerAlarmService_ = ((TimerAlarmService.TimerAlarmBinder)
service).getService();
}

@Override
public void onServiceDisconnected(
ComponentName className)
{
timerAlarmService_ = null;
}
};


・ActivityではonCreateメソッドで下記のようにServiceを初期化
// 通知用のサービスを生成
Intent intent = new Intent(this, TimerAlarmService.class);
startService(intent);
IntentFilter passageFilter = new IntentFilter(
AlarmTimerConstants.BROADCAST_PASSAGE);
IntentFilter lastFilter = new IntentFilter(
AlarmTimerConstants.BROADCAST_LAST);
IntentFilter alarmFilter = new IntentFilter(
AlarmTimerConstants.BROADCAST_ALARM);
registerReceiver(this.receiver_, passageFilter);
registerReceiver(this.receiver_, lastFilter);
registerReceiver(this.receiver_, alarmFilter);
// サービスにバインド
bindService(intent, this.connection_,
Context.BIND_AUTO_CREATE);

上記の状況で、サービス起動後にボタンを押したタイミングにおいて、
未だonServiceConnectedが呼ばれない状態となっています。
#起動後、数十秒たってます。

また、manifestでも下記のようにserviceは有効化しています。
<service android:enabled="true" android:name=".TimerAlarmService" />

情報を探してみたのですが、
・即bindされるわけではないため、onCreateの中でbind→service取得処理は難しい
といった情報しか見つからず、そもそもonServiceConnectedが呼ばれない場合の対処は
わかっていない状態です。

もしご存知の方がいらっしゃれば、解決の糸口をご教授お願いできないでしょうか。
よろしくお願いします。

tm sute

unread,
Nov 15, 2010, 1:02:10 AM11/15/10
to android-g...@googlegroups.com
tmsuteです。

たぶん、ですが、

(10/11/13 8:58), S. Kimura wrote:
> ・ActivityではonCreateメソッドで下記のようにServiceを初期化
> // 通知用のサービスを生成
> Intent intent = new Intent(this, TimerAlarmService.class);

略...........


> // サービスにバインド
> bindService(intent, this.connection_,
> Context.BIND_AUTO_CREATE);

この、bindServiceするintentが、間違っているのではないでしょうか?
自分が以前書いたコードは以下のようになっていました。

private boolean bindHogeHoge() {
if( m_HogeHogeServiceIf == null ) {
Intent intent_bind = new Intent(IHogeHoge.class.getName());
if( true != bindService(intent_bind, m_HogeHogeServiceConn, 0) ) {
// Error
stopService(new Intent(this, HogeHoge.class));
return(false);
}
}
return(true);
}


S. Kimuraさんのコードでは、bindServiceの第一引数に、Serviceの
intentをセットしているように見えますが、私はaidlから自動生成
されたinterfaceである、IHogeHogeを使っています。
bindServiceの本質として、interfaceをintentに使うのはまっとうで
あろうと勝手に解釈しています。(今reference読んだけど、意味が
わからんかった。。。笑)

それと、bindServiceの戻り値を是非チェックしてエラー処理を
書くようにしましょう。Serviceの場合結構重要だと思います。

S. Kimura

unread,
Nov 15, 2010, 5:06:59 PM11/15/10
to 日本Androidの会
S. Kimuraです。

tmsuteさん、返信ありがとうございます。

> S. Kimuraさんのコードでは、bindServiceの第一引数に、Serviceの
> intentをセットしているように見えますが、私はaidlから自動生成
> されたinterfaceである、IHogeHogeを使っています。
> bindServiceの本質として、interfaceをintentに使うのはまっとうで
> あろうと勝手に解釈しています。(今reference読んだけど、意味が
> わからんかった。。。笑)

調べた所ですと、同一プロセス内であればintentで出来るような例が
いくつか見つかって、それを参考にしていたのですが、
やはり邪道、ということでしょうか(笑

aidlから生成されたInterfaceを設定してまずは試してみます。


> それと、bindServiceの戻り値を是非チェックしてエラー処理を
> 書くようにしましょう。Serviceの場合結構重要だと思います。

こちらもありがとうございます。
APIを調べたところ、返り値で成功失敗がその時点で判断できるわけですね。
確認処理を追加して、また試してみます。

返信ありがとうございました。
結果がわかったらまた書き込みますね。

フクシー

unread,
Nov 28, 2010, 6:54:00 AM11/28/10
to 日本Androidの会
S. Kimura改めフクシーです。

中々時間が取れなくて間が空いてしまいましたが、
ようやく確認することが出来ました。

aidlから生成されたInterfaceを設定した所、
サービスにアクセスすることが可能なことを確認できました。

また、Serviceがbind出来ない状態では「bindService」の返り値もfalseとなっていました。
さっさか確認すれば、少なくともbind出来ていない、ということまでは早い段階でわかりましたね。。。

ともあれ、テストアプリも完成できそうです。
回答ありがとうございました。
Reply all
Reply to author
Forward
0 new messages