Android - 绑定服务

3

你好:我似乎无法使同一包中的活动绑定到服务。

该活动如下所示:

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始终为null,因此我无法执行诸如mService.isRunning()之类的操作)

服务的清单条目只是:

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

我从Android开发者网站直接复制了代码,但我可能漏掉了某些内容。
2个回答

25

众所周知,在TabActivity中作为子Activity启动的活动(在TabHost中运行)甚至不能绑定到服务,即使在同一个包中。有一个“hack”的解决方法。如果调用:

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

不仅仅是:

 bindService(Intent, connection, flags);

一切都会正常工作。我曾经遇到过同样的问题,可以在这个错误报告中找到详细信息: http://code.google.com/p/android/issues/detail?id=2483


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而不是0进行绑定时,您还可以自动启动服务。

如果您只想启动服务一次,则可以使用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;
     }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接