我发现的许多类似的问题都没有帮助解决我的问题。
下面是一些我看过的问题:
以下是我的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. */
};
正如您可以使用的那样,应该从myNetworkQueryServiceConnection方法(位于onCreate()中)中调用onCreate。这(理论上)也应该运行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>
我当然没有看到关于这项服务的任何东西,但我想我不知道如何添加它。
发布于 2015-04-19 21:24:12
您需要添加一个<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
-否则,在正确的完全限定类名中替换。
https://stackoverflow.com/questions/29735892
复制相似问题