0

I would like to know if I can start a service from a dialog.

Somehow my bindService function gives false.

Here is brief code.

Manifest file:

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

Here is 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;
        }

    }

And call from 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;

    }

};

Any clues why I always get return from bindService() as false?

When I try to start service from dialog I get some error like this too for my app

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

Also I tried through the activity itself to launch the Service. Still getting same problem.

tariq zafar
  • 659
  • 7
  • 24
  • Hi,thanks for reply. But then I moved starting service code to the activity itself and even then the return of bindService seems to be false. – tariq zafar Sep 25 '13 at 10:37
  • did u try :: getApplicationContext().bindService instead of mContext.bindservice. – ASP Sep 25 '13 at 10:39
  • Yeah I did try that too. I have edited a bit too in the above query. could it be I have missed something in manifest or something like that from this error "Failure getting entry for 0x010802c9 (t=7 e=713) in package 0 (error -75)" – tariq zafar Sep 25 '13 at 10:53
  • I think you need to start the service before you can bind to it, so the problem is more around the fact you don't seem to be able to start the service. If you are using eclipse, have you tried using Project -> Clean? – Ed_ Sep 25 '13 at 11:46

0 Answers0