Android Context.bindService 返回 false 问题

前段时间在项目中遇到一个问题,使用bindService绑定服务竟然返回false。

绑定失败,自然也就不会调用ServiceConnection的onServiceConnected方法。


对于bindService这种很直观的操作之前做过一些,从来没失败,这很是让我头疼。

后来在stackoverflow上找到了解决方法。


问题原因:

使用了Tabhost,其中的一个子Activity调用的bindService,导致失败,具体底层原因没有去看(我惭愧。。)


解决:

使用上一层的Context即可,暨不直接调用Context.bindService,而是Context.getApplicationContext().bindService.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的Android AIDL实例: 1. 在你的Android Studio项目中,创建一个新的模块,并选择“Android Library”类型。 2. 在该模块中,创建一个名为“IMyAidlInterface”的AIDL接口文件,并在其中定义要使用的方法。例如: ``` // IMyAidlInterface.aidl package com.example.myaidl; // Declare any non-default types here with import statements interface IMyAidlInterface { int add(int a, int b); } ``` 3. 在同一模块中,创建一个实现AIDL接口的类。例如: ``` // MyAidlInterfaceImpl.java package com.example.myaidl; import android.os.RemoteException; public class MyAidlInterfaceImpl extends IMyAidlInterface.Stub { @Override public int add(int a, int b) throws RemoteException { return a + b; } } ``` 4. 在你的应用程序中,创建一个Service,并在其中使用上面定义的AIDL接口和实现类。例如: ``` // MyService.java package com.example.myaidl; import android.app.Service; import android.content.Intent; import android.os.IBinder; public class MyService extends Service { private MyAidlInterfaceImpl myAidlInterfaceImpl; @Override public void onCreate() { super.onCreate(); myAidlInterfaceImpl = new MyAidlInterfaceImpl(); } @Override public IBinder onBind(Intent intent) { return myAidlInterfaceImpl; } } ``` 5. 在你的应用程序中,通过bindService()方法绑定MyService,并使用IMyAidlInterface接口中的方法。例如: ``` // MainActivity.java package com.example.myaidl; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { private static final String TAG = MainActivity.class.getSimpleName(); private IMyAidlInterface myAidlInterface; private boolean isBound = false; private ServiceConnection connection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { myAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder); isBound = true; } @Override public void onServiceDisconnected(ComponentName componentName) { myAidlInterface = null; isBound = false; } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button addButton = findViewById(R.id.add_button); addButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (isBound) { try { int a = 1; int b = 2; int result = myAidlInterface.add(a, b); Log.d(TAG, "Result: " + result); } catch (RemoteException e) { e.printStackTrace(); } } } }); } @Override protected void onStart() { super.onStart(); Intent intent = new Intent(this, MyService.class); bindService(intent, connection, Context.BIND_AUTO_CREATE); } @Override protected void onStop() { super.onStop(); if (isBound) { unbindService(connection); isBound = false; } } } ``` 这就是一个简单的Android AIDL实例,它演示了如何在应用程序中使用AIDL接口。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值