Android: onServiceConnected 在 bindService 之后没有被调用

标签 android serviceconnection bindservice 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. */

};

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

这是我第一次必须在 Android 中绑定(bind)服务,所以我确定我错过了一些东西。我希望有人能指出错误。

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

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

<?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 — 否则,替换为正确的完全限定类名。

关于Android: onServiceConnected 在 bindService 之后没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59138905/

相关文章:

android - Activity <App Name> 泄露了原来绑定(bind)在这里的ServiceConnection <ServiceConnection Name>@438030a8

azure - 使用 powershell 获取 Azure DevOps 服务连接服务主体 ID

android - 设置 IMediaPlaybackService

android - 当 Activity 崩溃时会发生什么?

android - TelephonyManager 在 android studio 上崩溃

Android ClassLoader.loadClass JNI 崩溃

java - 为什么光标索引越界?

android - 复制保护对免费应用有意义吗?