Android: onServiceConnected 未在 bindService 之后调用

Android: onServiceConnected not called after bindService

None 我发现的许多类似问题帮助解决了我的问题。

以下是我看过的一些问题:

这是我的 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. */

};

如您所知,应该从 bindService 方法(位于 onCreate() 中)调用 myNetworkQueryServiceConnection。这(理论上)也应该 运行 onServiceConnected(我认为),但我在我的代码中放置了一个断点,它永远不会在该方法内停止。我做了一些调试和 bindService returns false,所以由于某种原因它没有成功绑定它。

这是我第一次必须在 Android 中绑定服务,所以我确定我错过了一些东西。希望大家指出错误。

如果您遗漏任何信息,请告诉我——我不确定您可能需要什么,我真的不能 post 我的整个应用程序代码都在这里。

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

<?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>

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

您需要添加一个 <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>

我的示例元素假定 NetworkQueryService 实际上是 com.tek15.cellemrmonitor.NetworkQueryService — 否则,替换为正确的完全限定的 class 名称。