2011-02-26 94 views
3

嗨:我似乎无法获得绑定到同一个包中的服务的活动。Android - 绑定到服务

活动看起来是这样的:

public class myApp extends TabActivity { 
    static private String TAG = "myApp"; 
    private myService mService = null; 
    private ServiceConnection mServiceConn = new ServiceConnection(){ 
     public void onServiceConnected(ComponentName name, IBinder service) { 
      Log.v(TAG, "Service: " + name + " connected"); 
      mService = ((myService.myBinder)service).getService(); 
     } 

     public void onServiceDisconnected(ComponentName name) { 
      Log.v(TAG, "Service: " + name + " disconnected"); 
     } 
    }; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     doBind(); 
     Log.i(TAG, "Started (UI Thread)"); 

     // set content 
     setContentView(R.layout.main); 

     Resources res = getResources(); // Resource object to get Drawables 
     TabHost tabHost = getTabHost(); // The activity TabHost 

     ... add some tabs here.... 

     tabHost.setCurrentTab(0); 
    } 

    private void doBind(){  
     Intent i = new Intent(this,myService.class); 
     if(bindService(i, mServiceConn, 0)){ 
      Log.i(TAG, "Service bound"); 
     } else { 
      Log.e(TAG, "Service not bound"); 
     } 
    } 

} 

然后,服务:

public class myService extends Service { 
    private String TAG = "myService"; 

    private boolean mRunning = false; 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startid) { 
     Log.i(TAG,"Service start"); 

     mRunning = true; 

     Log.d(TAG,"Finished onStartCommand"); 
     return START_STICKY; 
    } 

    /* 
    * Called on service stop 
    */ 
    @Override 
    public void onDestroy(){ 
     Log.i(TAG,"onDestroy"); 
     mRunning = false; 
     super.onDestroy(); 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     return mBinder; 
    } 


    boolean isRunning() { 
     return mRunning; 
    } 

    /* 
    * class for binding 
    */ 
    private final IBinder mBinder = new myBinder(); 
    public class myBinder extends Binder { 
     myService getService() { 
      return myService.this; 
     } 
    } 
} 

bindService返回true,但onServiceConnection不会被调用(MSERVICE总是空的,所以我不能做什么像mService.isRunning())

该服务的清单条目只是:

<service android:name=".myService"></service> 

我是从Android开发人员网站直接复制代码,但我一定错过了一些东西。

回答

3

您的服务从未开始。先从 startService(bindIntent)

服务看到这个类似的代码:

Intent bindIntent = new Intent(this,ServiceTask.class); 
if (ServiceTools.isServiceRunning() == false){ 
    Log.d(Global.TAG,"-->service will be started."); 
    startService(bindIntent); 
}else{ 
    Log.d(Global.TAG,"-->service already is running"); 
} 
boolean bound = bindService(bindIntent,mConnection,0); 

这里有BIND_AUTO_CREATE而是结合时,您也可以自动启动该服务是ServiceTools类,如果你还只希望您的服务能够启动一次。否则,您可能会得到多个正在运行的服务实例,并且每次onCreate()/ doBind()时,在onServiceConnected中调用的方法都会变得很糟糕。

public class ServiceTools { 
    public static boolean isServiceRunning(){ 
     final ActivityManager activityManager = (ActivityManager)Global.gContext.getSystemService(Global.gContext.ACTIVITY_SERVICE); 
     final List<RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE); 

     boolean isServiceFound = false; 

     for (int i = 0; i < services.size(); i++) { 
      //Log.d(Global.TAG, "Service" + i + " :" + services.get(i).service); 
      //Log.d(Global.TAG, "Service" + i + " package name : " + services.get(i).service.getPackageName()); 
      //Log.d(Global.TAG, "Service" + i + " class name : " + services.get(i).service.getClassName()); 

      if ("com.atClass.lmt".equals(services.get(i).service.getPackageName())){ 
       if ("com.atClass.lmt.ServiceTask".equals(services.get(i).service.getClassName())){ 
        isServiceFound = true; 
       } 
      } 
     } 
     return isServiceFound; 
    } 
} 
+0

如果这对你有帮助,也接受答案。 – 2011-02-26 19:06:12

25

这是一个已知的问题,活动正式开始为在tabactivity(在tabhost运行)一个子活动不能绑定到服务,甚至在同一个包。有一个“黑客”解决方法。如果您致电:

getApplicationContext().bindService(Intent, connection, flags); 

,而不仅仅是:

bindService(Intent, connection, flags); 

一切都将正常工作。有这个相同的问题,发现在这个错误报告中的细节: http://code.google.com/p/android/issues/detail?id=2483