Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8325463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:26:42+00:00 2026-06-09T00:26:42+00:00

I’m trying to implement a service into my application, which is started by an

  • 0

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);

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T00:26:44+00:00Added an answer on June 9, 2026 at 12:26 am

    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" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.