Android – 绑定到服务

Android – 绑定到服务,第1张

概述嗨:我似乎无法将活动绑定到同一个包中的服务. 活动如下: public class myApp extends TabActivity { static private String TAG = "myApp"; private myService mService = null; private ServiceConnection mServiceConn = new S 嗨:我似乎无法将活动绑定到同一个包中的服务.

活动如下:

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开发者网站复制代码,但我一定错过了一些东西.

解决方法 这是一个已知的问题,即活动开始时作为tabactivity中的子活动(在tabhost中运行)无法绑定到服务,即使在同一个包中也是如此.有一个“黑客”的解决方法.如果你打电话:

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

而不仅仅是:

bindService(Intent,flags);

一切都会奏效.遇到同样的问题,在这个BUG报告中找到了详细信息:
http://code.google.com/p/android/issues/detail?id=2483

总结

以上是内存溢出为你收集整理的Android – 绑定到服务全部内容,希望文章能够帮你解决Android – 绑定到服务所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1123522.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存