Android:在bindService之后没有调用onServiceConnected

Der*_*om8 0 android serviceconnection bindservice onserviceconnected

首先,我要说的是,我已经在SO上发现了很多与此问题几乎相同的问题,但是没有一个(我能找到的)解决了这个问题.这些是我看过的一些:

ServiceConnection.onServiceConnected()在绑定到已启动的服务 之后永远不会调用onServiceConnected,在bindService方法之后从未调用过 ServiceConnection :: onServiceConnected,即使Context :: bindService返回true也未调用? OnServiceConnected未被调用

这是我的Android应用程序代码的一部分:

    //Local service interface
private INetworkQueryService myNetworkQueryService = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    init();
    Intent intent = new Intent(this, NetworkQueryService.class);
    startService (intent);
    bindService (new Intent(this, NetworkQueryService.class), myNetworkQueryServiceConnection,
                            Context.BIND_AUTO_CREATE);
}

//Service connection
private final ServiceConnection myNetworkQueryServiceConnection = new ServiceConnection() {
    /** Handle the task of binding the local object to the service */
    public void onServiceConnected(ComponentName className, IBinder service) {
        myNetworkQueryService = ((NetworkQueryService.LocalBinder) service).getService();
        // as soon as it is bound, run a query.
        loadNetworksList();
    }

    /** Handle the task of cleaning up the local binding */
    public void onServiceDisconnected(ComponentName className) {
        myNetworkQueryService = null;
    }

    public void loadNetworksList() {
        // delegate query request to the service.
        try {
            myNetworkQueryService.startNetworkQuery(myCallback);
        } catch (RemoteException e) {
        }
    }

};  

/**
 * This implementation of INetworkQueryServiceCallback is used to receive
 * callback notifications from the network query service.
 */
private final INetworkQueryServiceCallback myCallback = new INetworkQueryServiceCallback.Stub() {

    @Override
    public void onQueryComplete(List<NetworkInfo> networkInfoArray,
            int status) throws RemoteException {
        displayNetworks(networkInfoArray, status);
    }

    /** place the message on the looper queue upon query completion. */

};
Run Code Online (Sandbox Code Playgroud)

正如你可以sy,myNetworkQueryServiceConnection应该从bindService方法(位于onCreate())中调用.这(理论上)也应该在ServiceConnected上运行(我认为),但是我在代码中放了一个断点,它永远不会在该方法中停止.我做了一些调试,bindService返回false,所以由于某种原因它没有成功绑定它.

这是我第一次在Android中绑定服务,所以我确定我错过了一些东西.我希望有人可以指出一个错误.

如果您遗漏任何信息,请告诉我 - 我不确定您可能需要什么,我真的无法在此发布我的整个应用代码.

编辑:我一直在阅读有关在清单中注册服务的事情,但似乎无法弄清楚在哪里或如何做?这是该项目的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tek15.cellemrmonitor"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="19"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
Run Code Online (Sandbox Code Playgroud)

我当然看不到有关该服务的任何信息,但我想我不知道如何添加它.

Com*_*are 7

您需要添加一个<service>元素,作为元素的对等<activity>元素,作为以下子元素<application>:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tek15.cellemrmonitor"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="19"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name="NetworkQueryService" />
</application>
Run Code Online (Sandbox Code Playgroud)

我的示例元素假定它NetworkQueryService确实是com.tek15.cellemrmonitor.NetworkQueryService- 否则,用适当的完全限定类名替换.