android bindservice方法,Android bindservice方法返回false

android bindservice方法,Android bindservice方法返回false我想从另一个类(BaseExpandableListAdapter)的活动中调用一个方法。活动中的方法启动服务并调用bindService(,,)方法。但是,bindService方法总是返回false。我查了其他类似的帖子,但没有一个解决了我的问题。任何评论非常感谢。Androidbindservice方法返回false这里是BaseExpendableListAdapter类中,我调用该方法…

大家好,又见面了,我是你们的朋友全栈君。

我想从另一个类(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.net/140606.html原文链接:https://javaforall.net

(0)
上一篇 2022年5月27日 下午5:20
下一篇 2022年5月27日 下午5:20


相关推荐

  • 利用flex实现元素水平垂直居中[通俗易懂]

    利用flex实现元素水平垂直居中[通俗易懂]首先介绍一下flex布局。采用Flex布局的元素,称为Flex容器(flexcontainer),简称“容器”。它的所有子元素自动成为容器成员,成为flex项目(flexitem),简称“项目”。容器默认存在两根轴:水平的主轴(mainaxis)和垂直的交叉轴(crossaxis)。主轴的开始位置(与边框的交叉点)叫做mainstart,结束位置叫做mainend;交叉轴的开始…

    2022年6月7日
    46
  • 正则表达式匹配所有数字,包括带小数点的数字 包含限制小数位数、整数位数「建议收藏」

    正则表达式匹配所有数字,包括带小数点的数字 包含限制小数位数、整数位数「建议收藏」letreg=/^[+-]?(0|([1-9]\d*))(\.\d+)?$/g;

    2022年6月15日
    34
  • 实操!我「手搓」了一场1.4万人看过的AI人机辩论赛(内附教程)

    实操!我「手搓」了一场1.4万人看过的AI人机辩论赛(内附教程)

    2026年3月12日
    3
  • L2-026 小字辈(dfs深搜)「建议收藏」

    L2-026 小字辈(dfs深搜)「建议收藏」原题链接本题给定一个庞大家族的家谱,要请你给出最小一辈的名单。输入格式:输入在第一行给出家族人口总数 N(不超过 100 000 的正整数) —— 简单起见,我们把家族成员从 1 到 N 编号。随后第二行给出 N 个编号,其中第 i 个编号对应第 i 位成员的父/母。家谱中辈分最高的老祖宗对应的父/母编号为 -1。一行中的数字间以空格分隔。输出格式:首先输出最小的辈分(老祖宗的辈分为 1,以下逐级递增)。然后在第二行按递增顺序输出辈分最小的成员的编号。编号间以一个空格分隔,行首尾不得有多余空格。

    2022年8月8日
    7
  • gridlayout布局

    gridlayout布局浅谈android4.0开发之GridLayout布局分类: Android应用开发技巧2012-03-1123:51 3646人阅读 评论(1) 收藏 举报androidlayoutbuttonencoding框架编程作者:李响         本文重点讲述了自android4.0版本后新增的GridLayout网格布局的一些基本

    2022年6月1日
    59
  • Java创建文件和文件夹

    Java创建文件和文件夹例子。java代码:importjava.io.*;//导入所需的包publicclassIOTest{//类 publicstaticvoidmain(String[]args){//主程序,程序入口 Filefile=newFile(“D:\\Qiju_Li”); if(!file.exists()){//如果文件夹不存在 file.mk

    2022年6月18日
    34

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号