user8515823
user8515823

Reputation: 33

Background service stops as soon as the app closed

I am creating a messaging app for that I need a service to receive message even when the app is closed or open, I have a broadcast receiver to receive SMS and I created a service class and started in my mainActivity as

Intent mIntent=new Intent(this,BackgroundService.class);
startService(mIntent);

and in the manifest, I added the service as

 <service android:name=".BackgroundService"
            android:enabled="true"/>

broadcastReceiver class

@Override
public void onReceive(Context context, Intent intent) {


    Bundle intentExtras = intent.getExtras();
    if (intentExtras != null) {

        Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
        String smsMessageStr = "";
        for (int i = 0; i < sms.length; ++i) {
            SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) 
sms[i]);

            smsBody = smsMessage.getMessageBody().toString();
            address = smsMessage.getOriginatingAddress();


            smsMessageStr += "SMS FROM :" + address + "\n";
            smsMessageStr += "Message :" + smsBody + "\n";
        }
            Toast.makeText(context, smsMessageStr, 
Toast.LENGTH_LONG).show();

backgroundService.java

public class BackgroundService extends Service{

private SmsBroadcastReceiver mSmsBroadcastReceiver;
private IntentFilter mIntentFilter;
private static final int NOTIFICATION_ID = 999;


@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    mSmsBroadcastReceiver=new SmsBroadcastReceiver();
    mIntentFilter=new IntentFilter();
    mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
    mIntentFilter.setPriority(999);
    registerReceiver(mSmsBroadcastReceiver,mIntentFilter);

    return START_STICKY;
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}
}

I HAVE TRIED foreground service also as

 Notification notification = new NotificationCompat.Builder(this)
            .setContentTitle("TutorialsFace Music Player")
            .setTicker("TutorialsFace Music Player")
            .setContentText("My song")
            .setSmallIcon(R.mipmap.ic_launcher)
            .build();
        startForeground(NOTIFICATION_ID,
            notification);

but as soon as I close my app from recent apps the service also gets stopped and when I restart my app its the service gets starting

how can I make my service to live long even when the app is closed. Because my app is SMS based one so without service I cant receive the messages while my app is closed, any help will be appreciated, thanks in advance.

Upvotes: 1

Views: 1470

Answers (1)

rafaelje
rafaelje

Reputation: 29

Yes the service is working in the same thread with the app you can override the method onDestroy to launch your service again with his own thread.

@Override
public void onDestroy() {
    super.onDestroy();
    getApplicationContext().startService(new Intent(getApplicationContext(), BackgroundService.class));
}

Upvotes: 1

Related Questions