android bindservice方法,Android bindservice方法返回false
大家好,又见面了,我是你们的朋友全栈君。
我想从另一个类(BaseExpandableListAdapter)的活动中调用一个方法。活动中的方法启动服务并调用bindService(,,)方法。但是,bindService方法总是返回false。我查了其他类似的帖子,但没有一个解决了我的问题。任何评论非常感谢。Android bindservice方法返回false
这里是BaseExpendableListAdapter类中,我调用该方法的活动:
class myExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap _listDataChild;
private TextView myroutes_distance=null;
private TextView myroutes_time=null;
private TextView myroutes_speed=null;
public myExpandableListAdapter(Context context, List listDataHeader,
HashMap listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, final ViewGroup parent) {
MyActivity myactivity = new MyActivity();
myactivity.continue(_context.getApplicationContext()); // continue is the method that I’m calling which is within the activity
}
这里与方法继续活动:
public class MyActivity extends FragmentActivity implements
MyService.Callbacks{
boolean isBound = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void continue(Context ctx){
current_intent = new Intent(ctx.getApplicationContext(), MyService.class);
ctx.getApplicationContext().startService(current_intent); // This method works fine.
isBound = ctx.getApplicationContext().bindService(current_intent, mConnection, Context.BIND_AUTO_CREATE); // Here is where I have problem. isBound is always false.
}
public ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className,IBinder service) {
//
Myservice.LocalBinder binder = (MyService.LocalBinder) service;
myservice = binder.getServiceInstance(); //Get instance of your service!
myservice.registerClient(MyActivity.this); //Activity register in the service as client for callabcks!
}
}
public void setup(){
current_intent = new Intent(MyActivity.this, MyService.class);
startService(current_intent);
isBound = bindService(current_intent, mConnection, Context.BIND_AUTO_CREATE);
// both startService and bindService methods work fine here.
}
}
请注意,我用setup()方法中的类似命令,它工作得很好,但是当我在continue()方法中使用bindservice()方法时,绑定失败。
+0
你为什么在getChildView实例在MainActivity()? –
+0
那么因为否则,我无法从其他活动中引用MyActivity中的继续方法。 –
+1
首先,您不应该从其他活动中调用该方法,而是在引用该活动的适配器中,它是’_context’。其次,你应该从不**自己实例化活动。 –
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/140606.html原文链接:https://javaforall.cn
相关文章
- android studio不能输入中文_Android模拟器
- 《Android Studio开发实战 从零基础到App上线(第2版)》资源下载和内容勘误[通俗易懂]
- android toast全屏,Android Toast实现全屏显示
- android登录注册跳转的代码_Android开发代码
- React Native android 常见错误和解决方法
- mac是基于什么内核的操作系统_mac编译android源码
- Android传感器开发与智能设备案例实战_Android移动应用开发
- 开发时遇到监听的事件处理机制和SoundPool播放音效解决方法以及外部类的使用【Android】
- 【Android APT】注解处理器 ( 注解标注 与 初始化方法 )
- 【Android 逆向】启动 DEX 字节码中的 Activity 组件 ( 在 PathClassLoader 和 BootClassLoader 之间插入 DexClassLoader )
- 【Android Gradle 插件】LintOptions 配置 ③ ( LintOptions#error 方法配置 | Lint 问题 ID | 查询 Lint 问题 ID 列表 )
- 【Android Gradle 插件】LintOptions 配置 ④ ( Lint 问题 ID | fatal 方法配置 | ignore 方法配置 | warning 方法配置 )
- Android加载Gif图片的一般方法:Movie实现详解手机开发
- Android 进程回收之LowMemoryKiller原理篇详解手机开发
- Android Studio 导入项目 出现安装Error:Cause: failed to find target with hash string ‘android-23’ 等错误详解手机开发
- Android中的ProgressBar的android:indeterminate详解手机开发
- 永诺推出YN455无反微4/3相机新品:5英寸Android触屏 售3888元
- android分辨率适配的方法
- Android异步获取网络图片并处理导致内存溢出问题解决方法
- 解决Fedora14下eclipse进行android开发,ibus提示没有输入窗口的方法详解
- android图库竖屏不显示statusbar的解决方法
- Android拍照保存在系统相册不显示的问题解决方法
- Android中断线程的处理方法
- Android实现上传文件功能的方法
- Android提高之ListView实现自适应表格的方法
- Android天气预报之基于HttpGet对象解析天气数据的方法