1

onServiceConnected()メソッドを実行するのに問題があります。これは、アクティビティがサービスにバインドされていないことを意味します。

それはおそらく私が見逃した単純なことですが、最初からかなりの回数試しました。

どうぞ...

私のサービスクラス

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class QuickService extends Service {

private final IBinder mBinder = new QuickBinder(this);

@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

}

私のバインダークラス

import android.os.Binder;

public class QuickBinder extends Binder {

private final QuickService service;

public QuickBinder(QuickService service){
    this.service = service;
}

public QuickService getService(){
    return service;
}

}

そして...サービスにバインドしようとしているアクティビティ。

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;

public class QuickActivity extends Activity {

QuickService mService;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_connecting);
}

@Override
protected void onStart() {
    super.onStart();
    Intent intent = new Intent(this, QuickService.class);
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}

@Override
protected void onStop() {
    super.onStop();
    // Unbind from the service
        unbindService(mConnection);
    }

/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName className,
            IBinder service) {
        Logger.d("Connected!!! :D");
        // We've bound to LocalService, cast the IBinder and get LocalService instance
        QuickBinder binder = (QuickBinder) service;
        mService = binder.getService();
    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {
    }
};
}

また、マニフェストファイルで定義されたサービス-それが問題だと思った場合に備えて。

<service android:name=".QuickService"></service>

だから、私はここで何が間違っているのですか?onServiceConnected()メソッドが呼び出されないのはなぜですか?

4

2 に答える 2

1

次のように更新します

 <service android:name=".QuickService">
            <intent-filter>
                <action android:name=".QuickService .BIND" />
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </service>
于 2012-07-07T05:25:38.117 に答える
-1

書く代わりに:

Intent intent = new Intent(this, QuickService.class);
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

あなたは書ける:

startService(new Intent(QuickActivity.this, QuickService.class));

サービスを開始したい場所。

これがお役に立てば幸いです。

于 2012-07-07T05:36:41.960 に答える