【问题标题】:Android: onServiceConnected not called after bindServiceAndroid:在 bindService 之后未调用 onServiceConnected
【发布时间】:2015-04-19 21:08:19
【问题描述】:

我发现的许多类似问题都没有帮助解决我的问题。

以下是我研究过的一些问题:

这是我的 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,所以由于某种原因它没有成功绑定它。

这是我第一次在 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>

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

【问题讨论】:

  • 您是否在清单中正确注册了此服务? FWIW,这是一个示例应用程序,演示了 bindService() 用于本地服务:github.com/commonsguy/cw-omnibus/tree/master/Binding/Local
  • 我一直在对其他线程的回复中看到它弹出,但我无法弄清楚它会在清单中的哪个位置注册,以及如何注册。也许这就是我所缺少的?我即将将清单添加到原始帖子中
  • AndroidManifest.xml 添加

标签: android serviceconnection bindservice onserviceconnected


【解决方案1】:

您需要添加一个&lt;service&gt; 元素,作为&lt;activity&gt; 元素的对等元素,作为&lt;application&gt; 的子元素:

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

【讨论】:

  • 非常感谢! NetworkQueryService 实际上在 com.android.phone 中,所以我在清单中添加了以下内容:&lt;service android:name="com.android.phone.NetworkQueryService" /&gt; 解决了一个问题——bindService 现在返回 true。但是,我遇到了另一个(可能)不相关的问题:java.lang.RuntimeException: PhoneFactory.getDefaultPhone must be called from Looper thread。如果需要,我将深入研究并提出另一个问题。不过,我可能会接受这个答案,因为它在技术上确实解决了我最初提出的问题。谢谢@CommonsWare!
  • @derstrom8:“NetworkQueryService 实际上在 com.android.phone 中”——将大量核心 AOSP 代码复制到应用程序中通常不起作用。
  • 是的,我之前遇到过几个问题,但已经设法只修补了我需要的部分。我修复了 looper 问题(修复很简单),所以现在我专注于权限,其中之一是 CONNECTIVITY_INTERNAL。我担心这仅适用于系统应用程序,我将无法使用它(我'没有在权限列表中看到它)
  • @derstrom8:是的,CONNECTIVITY_INTERNALa signature|system permission
  • 这就是我害怕的。而且看起来唯一的方法就是使用有根设备,这对我来说不是一个选择.... =(
猜你喜欢
  • 2011-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多