Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Can';不要将活动绑定到服务_Android_Android Intent_Bind_Android Service - Fatal编程技术网

Android Can';不要将活动绑定到服务

Android Can';不要将活动绑定到服务,android,android-intent,bind,android-service,Android,Android Intent,Bind,Android Service,我在运行onServiceConnected()方法时遇到问题,这意味着它没有将我的活动绑定到服务 这可能是我错过的一些简单的事情——但我已经尝试了好几次——从零开始 我们走吧 我的服务级别 import android.app.Service; import android.content.Intent; import android.os.IBinder; public class QuickService extends Service { private final IBinder

我在运行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.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>


那么,我做错了什么?为什么没有调用OnServiceConnectiond()方法?

使用以下命令更新它

 <service android:name=".QuickService">
            <intent-filter>
                <action android:name=".QuickService .BIND" />
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </service>

代替书写:

Intent intent = new Intent(this, QuickService.class);
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
你可以写:

startService(new Intent(QuickActivity.this, QuickService.class));
您想在哪里开始服务


希望这能对您有所帮助。

谢谢,现在一切都正常了。我必须去看看你为什么现在需要添加它。你能解释一下我为什么需要添加它吗?是的,但是如果我想有一个你可以绑定的服务,那么我认为这在这里没有什么帮助。