我对bindService有个问题。在我的活动中,我有以下代码:
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);但是isConnected每次都等于false。
在我的清单文件中,我有:
<service android:name=".DownloaderService">
<intent-filter>
<action android:name=".IPrimary" />是的,我不明白问题所在。在logcat中出现:
I/ActivityManager( 52):显示的活动com.touristeye.code/.LogIn: 485918 ms (总计913151 ms)
谢谢
发布于 2010-02-18 08:26:31
将该action:name展开为<action>元素中的全值。点前缀速记可能只适用于组件元素(例如,<service>)。
发布于 2011-11-05 17:11:10
你不应该这样做:
boolean isConnected = bindService(new Intent(IPrimary.class.getName()), mConnection, Context.BIND_AUTO_CREATE);请在处理服务时将代码放在私有ServiceConnection mConnection = new ServiceConnection() {}中...我回调,你有服务来处理那里我们不确定什么时候服务真正绑定,直到我们从ServiceConnection得到回调
以下是流程
创建调用服务的意图。您可以对BIND_AUTO_CREATE使用startService()或BindService()
一旦服务被绑定,它将创建一个隧道来与它的客户端进行对话,这是IBinder接口。它由您的AIDL接口实现使用,并在
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接口来调用和检索其中的任何服务
https://stackoverflow.com/questions/2285210
复制相似问题