6

サービスをバインドしようとしていますが、onBind()常に false を返します。

これはServiceConnection-のコードです

private ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {
        // This is called when the connection with our service has been established, 
        // giving us the service object we can use to interact with our service.
        mBoundService = ((ScheduleService.ServiceBinder) service).getService();
    }

    public void onServiceDisconnected(ComponentName className) {
        mBoundService = null;
    }
};

bindService()これは-への呼び出しです

boolean test = getApplicationContext().bindService(new Intent(this, ScheduleService.class), mConnection, Context.BIND_AUTO_CREATE);

これは、マニフェストでのサービスの宣言です -

<service android:name=".Notifications.ScheduleService" android:enabled="true"/>

この件に関する以前の質問を読みましたが、答えが見つかりませんでした (アクティビティ コンテキストをアプリケーション コンテキストに切り替えようとしましたが、役に立ちませんでした)。

Frgaments と ActionBarSherlock を使用しており、アクティビティが拡張されていますSlidingFragmentActivity(そのため、アプリケーション コンテキストを使用していますが、これは役に立ちません)。

編集 - これは、開始しようとしているサービスのコードです -

public class ScheduleService extends Service {

    /**
     * Class for clients to access
     */
    public class ServiceBinder extends Binder {
        public ScheduleService getService() {
            return ScheduleService.this;
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("ScheduleService", "Received start id " + startId + ": " + intent);

        // We want this service to continue running until it is explicitly stopped, so return sticky.
        return START_STICKY;
    }

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

    // This is the object that receives interactions from clients. See
    private final IBinder mBinder = new ServiceBinder();

    /**
     * Show an alarm for a certain date when the alarm is called it will pop up a notification
     */
    public void setAlarm(Calendar c) {
        // This starts a new thread to set the alarm
        // You want to push off your tasks onto a new thread to free up the UI to carry on responding
        new AlarmTask(this, c).run();
    }
}

どんな助けでも大歓迎です。ありがとう。

4

3 に答える 3

1

アプリケーション コンテキストを使用してサービスをバインドするのはなぜですか? メソッド bindService は、ContextWrapper を通じて呼び出されます。それは問題ではないかもしれませんが、サービスをバインドする場所と接続がある場所でコンテキストを共有します。

あなたの場合、代わりに

boolean test = getApplicationContext().bindService(new Intent(this, ScheduleService.class), mConnection, Context.BIND_AUTO_CREATE);

私は次のことをします

boolean test = bindService(new Intent(this, ScheduleService.class), mConnection, Context.BIND_AUTO_CREATE);

または、アプリケーション内にグローバル コンテキストを保持する場合は、すべてをアプリケーション ファイルに移動し、上記で提案したのと同じ方法で同様に呼び出します。

この問題は、アプリのパッケージ名と、マニフェストでのサービスの宣言にもある可能性があります。不明な場合は、マニフェストでサービスへのグローバル ルートを指定してください。

于 2013-03-24T14:08:14.513 に答える
1

完全修飾クラス名ScheduleService(完全なパッケージ名を含む) は?

あなたの AndroidManifest.xml ファイルでは、あなたのサービスの名前は.Notifications.ScheduleService少し奇妙に思えるので、私はこれを求めています:

これは、次のいずれかを教えてくれます

  • パッケージ名 (の最後の部分) に大文字が含まれてい ます...あまり良くありません。これが事実である場合、
    私は代わりに期待します。.notifications.ScheduleService
  • ScheduleService、Notifications.java というファイル内で定義されています。.Notifications$ScheduleServiceこれが当てはまる場合は、代わりに期待します(ピリオドではなくドル記号)。
于 2013-03-19T22:27:11.727 に答える