从对话框创建服务

时间:2013-09-25 10:27:33

标签: android service

我想知道我是否可以从对话框中启动服务。

不知怎的,我的bindService函数给出了错误。

这是简短的代码。

清单文件:

    <service android:name="MyService">
            <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
    </service>

这是服务

    public class MyService extends Service {

        private final IBinder mBinder = new MainBinder();

        public class MainBinder extends Binder {         
            MyService getService() {             
                return MyService.this;
            }     
        } 

        MyService() {
            mContext = this;
            Log.d(LOG_TAG,"Constructor ");

        }

        public void onCreate() {
            Log.d(LOG_TAG,"entered onCreate ");
            super.onCreate();
        }

        public IBinder onBind(Intent arg0) {
            // TODO Auto-generated method stub
            return mBinder;
        }

        protected int OnStartCommand (Intent intent, int flags, int startId) {
            Log.d(LOG_TAG, "start service");

            return START_STICKY;
        }

    }

从Dialog打电话

public Dialog onCreateDialog(Bundle savedInstanceState) {

    this.mContext = getActivity();
    initService();
}   

private void initService() {
    boolean ret;
    Intent  i = new Intent(mContext, MyService.class);

    //mContext.startService(i);
    ret = mContext.bindService(i, myConnection, Context.BIND_AUTO_CREATE);

    Log.d(LOG_TAG, "initService() bound " + ret);
}

private ServiceConnection myConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName className,
            IBinder service) {

        MyService.MainBinder binder = (MyService.MainBinder) service;
        mBoundService = binder.getService();
        isBound = true;
        Log.d(LOG_TAG,"Bound to Service");
    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        isBound = false;

    }

};

任何线索为什么我总是从bindService()返回false?

当我尝试从对话框启动服务时,我的应用程序也出现了类似的错误

09-25 16:13:32.054: W/ResourceType(1220): Failure getting entry for 0x010802c9 (t=7 e=713) in package 0 (error -75)

此外,我尝试通过活动本身来启动服务。仍然遇到同样的问题。

0 个答案:

没有答案