
[英]Android Service Unable to start intent & NullPointerException
[英]Android RemoteService Unable to start Service Intent
我正在尝试构建一个远程服务,该服务可以绑定到经常使用和创建的活动。 我认为这是处理我要完成的最佳方法。 我一直收到此错误。
无法启动服务意图{act = com.services.OverheadService} U = 0:找不到
我认为清单可能有问题吗? 但是我不知道是什么,我的清单将在下面相关代码的底部。
这是我的活动中onCreate()中的调用:
// TESTING SERVICE IMPLMENTATION TODO
Intent serviceIntent = new Intent("com.services.OverheadService");
boolean ok = this.bindService(serviceIntent, mServiceConnection, Context.BIND_AUTO_CREATE);
Log.v("ok", String.valueOf(ok));
这是Connection方法:
/** SERVICE IMPLEMENTATION TESTING **/
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// get instance of the aidl binder
mRemoteService = IRemoteService.Stub.asInterface(service);
try {
String message = mRemoteService.sayHello("Mina");
Log.v("message", message);
} catch (RemoteException e) {
Log.e("RemoteException", e.toString());
}
}
};
这是服务类别:
package com.services;
import com.services.IRemoteService.Stub;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
public class OverheadService extends Service {
@Override
public IBinder onBind(Intent arg0) {
return mBinder;
}
// implementation of the aidl interface
private final IRemoteService.Stub mBinder = new Stub() {
@Override
public String sayHello(String message) throws RemoteException {
return "Hello " + message;
}
};
}
设置服务的AndroidManifest:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<service
android:name="com.services.OverheadService"
android:enabled="true"
android:icon="@drawable/failed_load" >
<!--
intent-filter>
<action android:name="com.cdkdevelopment.BaseActivity" />
</intent-filter>
-->
</service>
我找到了解决方案,它正在onCreate中对此进行了更改
Intent serviceIntent = new Intent(this, OverheadService.class);
boolean ok = this.getApplicationContext().bindService(serviceIntent,
mServiceConnection, Context.BIND_AUTO_CREATE);
Log.v("ok", String.valueOf(ok));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.