I am quite new to Android and I am porting a C# program to android. Most of it is going fine but I have a long running problem of not being able to bind to 1 of my services. I wanted to start and bind to the service to receive sendBroadcast(intent) from the service. The intent includes the data package for displaying in the UI using sendBroadcast(intent) but that returns a nullpointer, presumably because it is not connected to the activity properly. I have followed so many different tutorials including the suggestions on this site all of which seemed fairly logical and were reported working. Such as http://developer.android.com/reference/android/app/Service.html http://devblogs.net/2011/01/04/bind-service-broadcast-receiver/ http://www.androidcompetencycenter.com/2009/01/basics-of-android-part-iii-android-services/
The error appears to be that the service is not starting properly...see the stack trace below: ActivityThread.handleCreateService(ActivityThread$CreateServiceData) line: 2943 Service is null. onCreate or onStartCommand() do not get called in the service.
Unfortunately I cannot use handlers for this because the constructor of the Service class is not called from the activity. As you can see I have tried many things and done some serious reading but obviously I am still missing something. I will give you what version of code I am currently trying which produced the stack trace above. This code doesn’t buil past the activities oncreate()! BUT the bool tmp from bindservice() returns true??
The activity
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(D) Log.e(TAG, "+++ ON CREATE +++");
// Various view initializations here
Intent CuIntent = new Intent(this, Curve.class);
CuIntent.setAction(".Core.CURVE_SERVICE");
startService(CuIntent);
Boolean tmp;
tmp = bindService(CuIntent, CurveServiceConncetion, Context.BIND_AUTO_CREATE);
}
private ServiceConnection CurveServiceConncetion = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
CurveService = ((LocalBinder<Curve>) service).getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
CurveService = null;
}
};
The activity manifest xml file with the service
<application android:label="@string/app_name" android:icon="@drawable/pi_icon_t">
<activity android:label="@string/app_name" android:name=".BTUI" android:finishOnCloseSystemDialogs="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"></action>
<category android:name="android.intent.category.LAUNCHER"></category>
</intent-filter>
</activity>
<service android:name=".Core.Curve" />
</application>
The localBinder class (a separate class)is taken from another tutorial and is
public class LocalBinder<S> extends Binder {
private String TAG = "LocalBinder";
private WeakReference<S> mService;
public LocalBinder(S service){
mService = new WeakReference<S>(service);
}
public S getService() {
return mService.get();
}
}
The service looks like this
public class Curve extends Service
{
private IBinder mBinder = new LocalBinder<Curve>(this);
@Override
public void onCreate() {
super.onCreate();
String balls = "balls";
Toast.makeText(this, "Curve Service created...", Toast.LENGTH_LONG).show();
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return mBinder;//
}
@Override
public void onStart(Intent intent, int startId) {
String balls = "balls";
Toast.makeText(this, "Curve Service created...", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
mBinder = null;
}
This really can't be that hard I hope someone can see where I am going wrong.
EDIT I picked a bad name for this post. The bind returns true but it crashes immediately after leaving the onCreate() of the activity. The error text is in the intro above.