1

My Broadcast receiver is working properly inside the Service until my application is running but immediately stops working if I close my application.

Here, I want my application to detect internet change and show a Toast every time but the Toast messages are shown until my application is working.

I've started my service in the MainActivity and defined my broadcast inside my service.

Inside onCreate in MainActivity-

Intent service = new Intent(MainActivity.this, LatestCallService.class);
    startService(service);

BroadcastReceiver inside the service:

         BroadcastReceiver br = new BroadcastReceiver()
    {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "Inside", Toast.LENGTH_SHORT).show();
 }
   }

  IntentFilter filter = new IntentFilter();
    filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
    _mContext = getApplicationContext();
    _mContext.registerReceiver(br, filter);

Please let me know what is wrong with my code.

saeed foroughi
  • 1,662
  • 1
  • 13
  • 25
tenTen
  • 55
  • 8

2 Answers2

1

the term "closing my application is not a percise statement. but I guess you mean that you swiped your app to right/left in the recent-tasks list screen.

Once onReceive() returns, if you do not have an activity in the foreground and you do not have a running service, your process importance will drop to what Android document calls it "cached process". The Android system will terminate these processes at any point. Once your process is terminated, your BroadCast receiver won't work either.

ok so if by closing app you didn't mean what I said you might mean when you are going to app's screen in Settings and clicking the Force Stop button. so, once your app is force stopped your app will never run, until the user or another app launcher your application!

********
Update with a solution: you can find a solution for your problem in this post.

saeed foroughi
  • 1,662
  • 1
  • 13
  • 25
  • Yes, I mean that I swiped my application right/left from the recent task list. My broadcast receiver is inside service, so I think it should work in the background, right? – tenTen Jan 21 '20 at 17:55
  • not necessarily. if you want a service that sort of lives forever and ever you could look here: https://stackoverflow.com/questions/20592366/the-process-of-the-service-is-killed-after-the-application-is-removed-from-the-a – saeed foroughi Jan 21 '20 at 18:35
  • @tenTen did this solved your problem? or you need any more help? – saeed foroughi Jan 22 '20 at 13:15
  • Yes! but I started the foreground activity.. and few more changes. – tenTen Jan 22 '20 at 17:19
  • It would be helpful for others after you, if you put your solution here – saeed foroughi Jan 22 '20 at 17:46
1

I've changed my code. I've stared the foreground service after which my code is working and broadcast receiver also working even after closing the application.

Foreground service in main Activity-

  Intent serviceIntent = new Intent(this, LatestCallService.class);
  serviceIntent.putExtra("inputExtra", "Foreground Service");
  ContextCompat.startForegroundService(this, serviceIntent);

Android Manifest file-

  uses-permission android:name="android.permission.FOREGROUND_SERVICE"

This way you can see whether your service is working in the background or not. Hope that helps!! :)

tenTen
  • 55
  • 8