Android 绑定服务方法 returns 错误

Android bindservice method returns false

我正在尝试从另一个 class (BaseExpandableListAdapter) 中调用一个 activity 中的方法。 activity中的方法启动一个服务,调用bindService(,)方法。但是 bindService 方法总是 returns false。我检查了其他类似的帖子,但其中 none 解决了我的问题。非常感谢任何评论。

这里是 BaseExpendableListAdapter class,我在其中调用 activity:

中的方法
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

}

这里是 Activity 和 continue 方法:

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() 方法时,绑定失败。

嗯,在我从@Luksprog 得到回复后,我意识到 bindService 方法 returns 错误的原因是因为我从另一个 class 实例化了 MainActivity class,这这不是个好主意!相反,我可以使用 LocalBroadcastManager 调用 continue() 方法。解决方案是这样的:

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 中 class:

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. 


}
}