首先,我要说我在SO上发现了很多与此问题几乎完全相同的问题,但是没有一个(我能找到的)解决了这个问题。这些是我看过的一些内容:
ServiceConnection.onServiceConnected() never called after binding to started service onServiceConnected never called after bindService method ServiceConnection::onServiceConnected not called even though Context::bindService returns true? OnServiceConnected not getting called
以下是我的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. */
};
可以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>
我当然没有看到有关该服务的任何信息,但我想我不知道如何添加它。
答案 0 :(得分: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>
我的示例元素假设NetworkQueryService
确实是com.tek15.cellemrmonitor.NetworkQueryService
- 否则,请用适当的完全限定类名替换。