Android绑定服务问题
我的服务有问题。在我的活动中,我有以下代码:Android绑定服务问题,android,service,Android,Service,我的服务有问题。在我的活动中,我有以下代码: private ServiceConnection mConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder service) { mService = IPrimary.Stub.asInterface(service); }
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder service) {
mService = IPrimary.Stub.asInterface(service);
}
public void onServiceDisconnected(ComponentName className) {
mService = null;
}
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.login);
mApi = new ApiRequest(SIGNIN_METHOD);
boolean isConnected = bindService(new Intent(IPrimary.class.getName()),
mConnection, Context.BIND_AUTO_CREATE);
但每次断开连接都等于错误
在我的清单文件中,我有:
<service android:name=".DownloaderService">
<intent-filter>
<action android:name=".IPrimary" />
是的,我不明白这个问题。在logcat中显示:
I/ActivityManager(52):显示的activity com.touristeye.code/。登录:485918毫秒(总计913151毫秒)
谢谢将
操作:name
扩展为
元素中的完整值。可能是点前缀速记仅适用于组件元素(例如,
)。您不应该这样做:
boolean isConnected = bindService(new Intent(IPrimary.class.getName()), mConnection, Context.BIND_AUTO_CREATE);
在私有ServiceConnection mConnection=newserviceconnection(){}
中处理服务时,请输入代码。。。我再打给你,你就可以在那里处理服务了
在从ServiceConnection收到回调之前,我们不确定服务何时实际绑定
这里是流程
创建调用服务的意图。您可以使用BIND_AUTO_CREATE启动服务()或BindService()
一旦该服务被绑定,它将创建一个隧道来与it客户(即IBinder)进行对话
接口。这将由AIDL接口实现使用,并在中返回IBinder
private final MyServiceInterface.Stub mBinder = new MyServiceInterface.Stub() {
public int getNumber() {
return new Random().nextInt(100);
}
};
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(this, "Service OnBind()", Toast.LENGTH_LONG).show();
return mBinder;
}
一旦它返回mBinder,您在客户机中创建的ServiceConnection将被回调,您将通过使用此命令获得服务接口
mConnection = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
mService = MyServiceInterface.Stub.asInterface(service);
};
现在,您有了mService接口,可以从该接口调用和检索任何服务