首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android bindservice方法返回false

Android bindservice方法返回false
EN

Stack Overflow用户
提问于 2015-09-12 01:54:06
回答 1查看 1.2K关注 0票数 0

我试图在来自另一个类(BaseExpandableListAdapter)的活动中调用一个方法。活动中的方法启动一个服务并调用bindService(,,)方法。但是,bindService方法总是返回false。我查过其他类似的帖子,但都没有解决我的问题。任何评论都是非常感谢的。

下面是在活动中调用方法的BaseExpendableListAdapter类:

代码语言:javascript
复制
class myExpandableListAdapter extends BaseExpandableListAdapter {
        private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, String> _listDataChild;
    private TextView myroutes_distance=null;
    private TextView myroutes_time=null;
    private TextView myroutes_speed=null;


    public myExpandableListAdapter(Context context, List<String> listDataHeader,
            HashMap<String, String> 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

}

下面是使用continue方法的活动:

代码语言:javascript
复制
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()方法中使用了类似的命令,它运行得很好,但是当我在连续()方法中使用bindservice()方法时,绑定失败了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-12 23:15:40

在我从@Luksprog得到响应之后,我意识到bindService方法返回false的原因是因为我从另一个类实例化了MainActivity类,这不是一个好主意!相反,我可以使用LocalBroadcastManager调用can ()方法。解决办法是这样的:

代码语言:javascript
复制
class myExpandableListAdapter extends BaseExpandableListAdapter {
        private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, String> _listDataChild;
    private TextView myroutes_distance=null;
    private TextView myroutes_time=null;
    private TextView myroutes_speed=null;


    public myExpandableListAdapter(Context context, List<String> listDataHeader,
            HashMap<String, String> 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



                  Intent intent = new Intent("intent_tag");

                          intent.putExtra("message", childText);
//                        

                          LocalBroadcastManager.getInstance(_context).sendBroadcast(intent);

}

在MainActivity类中:

代码语言:javascript
复制
public class MyActivity extends FragmentActivity implements
 MyService.Callbacks{

boolean isBound = false;

@Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
  LocalBroadcastManager.getInstance(this).registerReceiver(myBroadcastReceiver,
              new IntentFilter("intent_tag"));


}

private BroadcastReceiver myBroadcastReceiver = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
        // Get extra data included in the Intent
        String name = intent.getStringExtra("message");
        continue(name);
      }
    };

public void continue(String input){

         // Process data based on the input string. 
         //
         // Mymethod(input);
        current_intent = new Intent(getApplicationContext(), MyService.class);

        getApplicationContext().startService(current_intent); 

    getApplicationContext().bindService(current_intent, mConnection, Context.BIND_AUTO_CREATE); 

 }
    public ServiceConnection mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className,IBinder service) {
//            
            isBound=true;
            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. 


}
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32534370

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档