0

我想從另一個類(BaseExpandableListAdapter)的活動中調用一個方法。活動中的方法啓動服務並調用bindService(,,)方法。但是,bindService方法總是返回false。我查了其他類似的帖子,但沒有一個解決了我的問題。任何評論非常感謝。Android bindservice方法返回false

這裏是BaseExpendableListAdapter類中,我調用該方法的活動:

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 

} 

這裏與方法繼續活動:

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()? – Luksprog

+0

那麼因爲否則,我無法從其他活動中引用MyActivity中的繼續方法。 – user1512681

+1

首先,您不應該從其他活動中調用該方法,而是在引用該活動的適配器中,它是'_context'。其次,你應該從不**自己實例化活動。 – Luksprog

回答

0

那麼從@Luksprog得到的響應後,我意識到bindService方法返回false的原因是因爲我從另一個類實例化了MainActivity類,這不是一個好主意!相反,我可以用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類:

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. 


} 
}