HerpDerpington
HerpDerpington

Reputation: 4473

Check wheter service has to start after device reboot

I'm trying to implement a service into my application, which is started by an activity. The service if the service has been started, it should run in the background until it is manually stopped (by the activity). The service has to check every incoming SMS for a special string. If the message contains the string the service should respond by sending another SMS (like a Automated Response System (ARS)). The first problem is that the services is stopped by the system after ca. 60 minutes. Furthermore the service has to be started after boot if it was running before the system was shut down. Here's the code from the service: (I'm sorry for the german comments. I had no time to translate them.)

package [...]
import [...]

public class SMSReaderService extends Service {

public  static String searchString;
private SMSreceiver mSMSreceiver; 
private IntentFilter mIntentFilter; 
private static Messenger outMessenger;
private final Messenger inMessenger = new Messenger(new IncomingHandler());

@Override
public void onCreate() {
    super.onCreate();

    //SMS-received-event receiver 
    mSMSreceiver = new SMSreceiver(); 
    mIntentFilter = new IntentFilter(); 
    mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED"); 
    registerReceiver(mSMSreceiver, mIntentFilter);

    // Loggen, dass das Objekt erstellt wurde
    Log.d("SMSReaderService::Created", "SMSReaderService has been Created.");
}

@Override
public IBinder onBind(Intent intent) {
    [...] 
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    searchString = (String) intent.getExtras().get("SERCHVALUE");

    // Loggen, dass der Service gestartet wurde
    Log.d("SMSReaderService::Started", "SMSReaderService has Started.");

    return ???;

}

@Override
public void onDestroy() {
    // Unregister the SMS receiver
    unregisterReceiver(mSMSreceiver);

    // Loggen, dass das Objekt zerstört wurde
    Log.d("SMSReaderService::Object::Destroyed", "SMSReaderService has been destroyed.");

    super.onDestroy();
} 

private class SMSreceiver extends BroadcastReceiver {
    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
        Bundle extras = intent.getExtras(); 
        if ( extras != null ) 
        { 
            [...]
        } 
    } 
}
}

EDIT: Now the service returns START_STICKY. I thougth this would work. (Actually it does, but an error occurs). After 1 hour the logcat shows the following information:

08-03 16:31:29.345: E/AndroidRuntime(9885): java.lang.RuntimeException: Unable to start service de.[...].SMSReaderService@41912c88 with null: java.lang.NullPointerException
08-03 16:31:29.345: E/AndroidRuntime(9885):     at de.[...].SMSReaderService.onStartCommand(SMSReaderService.java:161)

08-03 16:32:20.940: E/AndroidRuntime(9933): java.lang.RuntimeException: Unable to start service de.[...].SMSReaderService@41913a30 with null: java.lang.NullPointerException
08-03 16:32:20.940: E/AndroidRuntime(9933):     at de.[...].SMSReaderService.onStartCommand(SMSReaderService.java:161)

In the "Anwendungsmanager" (I don't know how it's called in english... Maybe something like "application manager") I can see the the Service running... for 1 second. Then it stops immediatly and tries to restart. After 2 tries it runs. This was while the logcat logs the errors.

Line 161: searchString = (String)intent.getExtras().get(GET_SEARCHSTRING_AFTER_START);

Upvotes: 2

Views: 1379

Answers (1)

nicholas.hauschild
nicholas.hauschild

Reputation: 42849

The documentation suggests that you want to return Service.START_STICKY from your onStartCommand() method. This will start your service again when resources are available, if it has previously been killed by the runtime.

As for starting your service on boot, you can create a BroadcastReceiver to listen for the system boot intent that is sent by the runtime. When the Intent is received by your BroadcastReceiver, you can then start your Service. If you need to only start the Service after the device has been shutdown, you can do something similar to listen for the shutdown intent and persist the state of your Service (running or not).

An additional note, you will need to enable a BootCompleted permission.

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Upvotes: 3

Related Questions