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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • RELU激活函数作用「建议收藏」

    RELU激活函数作用「建议收藏」梯度消失现象:在某些神经网络中,从后向前看,前面层梯度越来越小,后面的层比前面的层学习速率高。梯度消失原因:sigmoid函数导数图像导数最大为0.25<1权重初始值通常使用一个高斯分布所以|w|<1,所以wjσ′(zj)<0.25,根据链式法则计算梯度越来越小。由于层数的增加,多个项相乘,势必就会导致不稳定的情况。sigmoid激活函数的…

    2022年6月16日
    34
  • Java之Java特点

    Java之Java特点

    2021年7月21日
    60
  • Python 奇淫技巧 — 利用pandas读取xml转换为excel

    Python 奇淫技巧 — 利用pandas读取xml转换为excel因为工作需要,将xml中特定的节点值取出来,然后统计到excel中。于是乎试试写了一个python脚本,加快工作效率。而且今后还能复用。以下为完整示例,需要的朋友们可参考。示例XML<?xmlversion=”1.0″encoding=”utf-8″?><breakfast_menu><food><name>BelgianWaffles</name><price>$5.95

    2022年8月22日
    15
  • leetcode题不会做怎么办_伪装学渣题目答案

    leetcode题不会做怎么办_伪装学渣题目答案在工程效率大行其道的今天,如果不会写点代码以后也不容易在测试圈混下去。今天给大家推荐一个LeetCode的刷题利器,可以伪装到连你老板在这里走过去都无法确认你是在干活呢,还是在干活呢。LeetCod

    2022年8月1日
    4
  • JetBrains又出神器啦,Fleet,体验飞一般的感觉「建议收藏」

    JetBrains又出神器啦,Fleet,体验飞一般的感觉「建议收藏」文章目录简介从eclipse到FleetFleet的特性JetBrainsSpace总结简介java开发的同学可能对于JetBrains这家公司并不陌生,因为JetBrains号称拥有世界上最好的JAVA开发工具IDEA。确实IDEA非常好用,它满足了一个java开发者所有的梦想。当然JetBrains还提供了其他语言的开发神器,PyCharm,PhpStrom,WebStorm等等。只要跟开发工作有关的,都能在JetBrains的全家桶中找到。这么好用的神器自然是价格不菲,但是JetBrains

    2022年5月1日
    389
  • java NIO字符集Charset 支持的所有字符集

    java NIO字符集Charset 支持的所有字符集可以通过以下方法打印所有的字符集publicvoidtest(){Map<String,Charset>map=Charset.availableCharsets();Set<Entry<String,Charset>>set=map.entrySet();for(Entry…

    2025年7月20日
    3

发表回复

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

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