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.