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


相关推荐

  • NGUI中穿插粒子或者mesh渲染层级

    NGUI中穿插粒子或者mesh渲染层级

    2022年2月22日
    38
  • python django 数据库_Apache+Mysql+PHP/Python简单项目

    python django 数据库_Apache+Mysql+PHP/Python简单项目基于Python+Django+mysql的实验室设备管理系统当今时代是飞速发展的信息时代,在各行各业中离不开信息处理,这正是计算机被广泛应用于信息管理系统环境原因。计算机的最大好处在于利用它能够进行信息管理。使用计算机进行信息控制,不仅提高了工作效率,而且大大的提高了其安全性。尤其对于复杂的信息管理,计算机能够充分发挥它的优越性。计算机进行信息管理与信息管理系统的开发密切相关,系统的开发是系统管理的前提。高校的实验室设备管理水平和实验室设备管理规模日益成为反映高校综合实…

    2022年10月9日
    1
  • VS2005 常用快捷键

    VS2005 常用快捷键

    2021年7月27日
    52
  • Watcher分集介绍(Weight Watcher)

    TextWatcher是packageandroid.text包下的一个接口。这个接口继承了另外一个接口NoCopySpan。TextWatcher接口里主要是定义了三个抽象方法:下面是它的基本用法:TextWatcherfieldValidatorTextWatcher=newTextWatcher(){      @Override   

    2022年4月18日
    243
  • iReport 设计介绍「建议收藏」

    iReport 设计介绍「建议收藏」目录iReport用户手册………………………………………………………………………………………………1iReport用户手册-介绍………………………………………………..

    2022年9月10日
    3
  • 微信小程序实现下载功能(以下载视频为例)「建议收藏」

    微信小程序实现下载功能(以下载视频为例)「建议收藏」一、wx.downloadFile()方法:访问视频对应的Url,回调函数返回一个该视频文件的临时路径。wx.downloadFile({url:app.serverUrl+me.data.videoInfo.videoPath,success:function(res){//只要服务器有响应数据,就会把响应内容写入文件并进入success回调,业务需要自行判断是否下载到了想要的内容

    2022年6月22日
    313

发表回复

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

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