0

I have a service that is running in the foreground, inside this service i have a broadcast receiver, that listen to the SMS_RECEIVED action.

When the user is inside the application (both the application and the service are in the foreground) everything works well, and i am receiving the intent.

But when the user exists the application (only the service with the broadcast is in the foreground), the service stops when sms is received.

When the service is stopped no error reports are found anywhere (not in the logcat and no crash dialog pops up).

My service with the broadcast:

public class myService extends IntentService {
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(myService.this, intent.getAction(), Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
        IntentFilter i= new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
        i.setPriority(999);
        registerReceiver(mReceiver, receiverFilter);
        startForeground(...);
    }

    public void onDestroy() {
        super.onDestroy();
        stopForeground(true);
    }
}

And i also have the following permission in my manifest:

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

myService is declared like this in the manifest:

<service
        android:name=".Services.myService"
        android:enabled="true"
        android:exported="false" />
user12597562891
  • 141
  • 1
  • 2
  • 7
  • Can you show logcat with error? – Master Disaster Aug 22 '16 at 08:57
  • Hey @MasterDisaster, there logcat show no error. – user12597562891 Aug 22 '16 at 09:19
  • `foreground service crash` so crash or not crash. If crash you should have error in logcat. – Master Disaster Aug 22 '16 at 09:21
  • It crashes (the service stops) but there are no errors in the logcat. – user12597562891 Aug 22 '16 at 09:31
  • If there's nothing in the logcat, and no dialog pops up, how exactly are you determining that it's crashing? – Mike M. Aug 22 '16 at 10:37
  • When a service is in the foreground you have to create a notification for him, i can see that the notification is removed when the sms is received meaning that the service itself is removed. – user12597562891 Aug 22 '16 at 11:04
  • 1
    Oh, I just saw this - `myService extends IntentService`. You want to use a regular `Service` for this. `IntentService`s are meant to handle finite tasks, and then stop themselves. (Btw, when there's more than one other user in the comments, no one will get notified of your comments unless you address them to a specific user with @username. I just happened to come back to check. Just FYI. Wasn't ignoring you. :-) – Mike M. Aug 22 '16 at 13:16
  • @MikeM. create an answer from your comment. The problem is `extends IntentService`. – David Wasser Aug 24 '16 at 21:28
  • @MikeM. I changed from IntentService to Service but the problem still exists.. any idea what else might be wrong? – user12597562891 Aug 25 '16 at 11:24

1 Answers1

0

I had the same problem and I solved it removing <uses-permission android:name="android.permission.RECEIVE_SMS" />. But removing this permission I can't detect incoming SMS, so I created a class like these Catching Outgoing SMS using ContentObserver and used MESSAGE_TYPE_RECEIVED = 1 instead MESSAGE_TYPE_SENT = 2. You need to add this permission <uses-permission android:name="android.permission.READ_SMS" />.

Community
  • 1
  • 1
Cambui
  • 1
  • 1
  • 2