Reputation: 437
I'm creating a background service (in its own process) and am having a lot of trouble getting it to work. I'm trying to launch it when the application starts, and I'm getting the unable to start service with intent error in the log. I've been going through forums, examples, (and google) and couldn't find what I'm doing wrong.
Here is the error I'm getting:
E/AndroidRuntime(1398): java.lang.RuntimeException: Unable to start service com.test.alarms.AlarmService@41550cb0 with Intent { cmp=xxxx }: java.lang.NullPointerException
In the activity I have:
startService(new Intent(AlarmService.class.getName()));
The service class is:
package com.test.alarms;
public class AlarmService extends Service{
Context context;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
//code to execute when the service is first created
}
@Override
public void onDestroy() {
//code to execute when the service is shutting down
}
@Override
public void onStart(Intent intent, int startid) {
//code to execute when the service is starting up
Intent i = new Intent(context, StartActivity.class);
PendingIntent detailsIntent = PendingIntent.getActivity(this, 0, i, 0);
NotificationManager notificationSingleLine = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notificationDropText = new Notification(R.drawable.ic_launcher, "Alarm for...", System.currentTimeMillis());
CharSequence from = "Time for...";
CharSequence message = "Alarm Text";
notificationDropText.setLatestEventInfo(this, from, message, detailsIntent);
notificationDropText.vibrate = new long[] { 100, 250, 100, 500};
notificationSingleLine.notify(0, notificationDropText);
}
}
The manifest file has:
<service
android:name=".AlarmService"
android:process=":remote">
<intent-filter>
<action android:name="com.test.alarms.AlarmService"/>
</intent-filter>
</service>
Thanks,
Upvotes: 1
Views: 9891
Reputation: 40136
Acording to the documentation, you need to check whether passing intent is null (for restarting after process killed) or not (normal operation) inside you onStartCommand(Intent intent,int,int);
Better design,
public int onStartCommand(Intent intent, int flags, int startId) {
if(intent != null){
handleCommand(intent);
}
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
Read here more
Upvotes: 5
Reputation: 24820
Maybe the problem is you have overridden OnCreate and OnDestroy but you are not calling the super.Oncreate() and super.onDestroy().
If this does not work try
startService(new Intent(context , AlarmService.class));
Edit: Also use this in onStart
Intent i = new Intent(this, StartActivity.class);
instead of
Intent i = new Intent(context, StartActivity.class);
Upvotes: 5