0

I have created a broadcast receiver to listen for connectivity change events. Receiver works perfectly when I minimize the app using home button. But when I use back button to exit the app broadcast receiver stops listening for connectivity changes. I searched for it and found that I should call broadcast receiver from a service but that too is not working on app close. Here is my code for broadcast receiver with service.

Internet Service

 public class InternetService extends Service {

    NetworkChangeReceiver networkChangeReceiver;

    public InternetService() {
        super();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        networkChangeReceiver = new NetworkChangeReceiver();
        registerReceiver(networkChangeReceiver, filter);
        return Service.START_STICKY;
    }

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

Broadcast receiver

public class NetworkChangeReceiver extends BroadcastReceiver {
    boolean isConnected = false;

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

    }


    private boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager)
                context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (info != null) {
                if (info.isConnected()) {
                    if (!isConnected) {
                        isConnected = true;
                        Toast.makeText(getBaseContext(), "Connected to internet", Toast.LENGTH_LONG).show();
                        Log.v("Internet", "Internet Connected");
                        hitDataService();
                        Intent intent = new Intent(ActivityForm.this, MyService.class);
                        startService(intent);
                    }
                    return true;
                }
            }
        }
        isConnected = false;
        return false;
    }
}

I want to send some data to service even on app close and when mobile is connected to internet. Is there any other way of getting same behaviour.

I also tried adding following lines in onCreateMethod() but still no luck

ComponentName receiver = new ComponentName(getBaseContext(), NetworkChangeReceiver.class);
                                                    PackageManager pm = getBaseContext().getPackageManager();
                                                    pm.setComponentEnabledSetting(receiver,
                                                            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                                                            PackageManager.DONT_KILL_APP);
Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84

2 Answers2

0

Here is the Sample Code that successfully processes alarm events, even after the process has been terminated by DDMS.

You can check the implementation of Broadcast Receiver in above sample that works even after app close.

Hope this will help you.

Rahul Parihar
  • 640
  • 7
  • 16
  • In this example they are using alarm manager object and a pending indent . Can I use it for checking connectivity changes also?? – Vivek Mishra Oct 03 '15 at 08:43
0

If You want to make the app listen for NetworkChangeReceiver broadcasts after you close the app, make sure you run InternetService before the app get closed.

Assuming your main activity is called MainActivity you can do like this:

@Override
protected void onDestroy() {
    super.onDestroy();
    Intent intent = new Intent(MainActivity.this, InternetService.class);
    startService(intent);
}

For NetworkChangeReceiver this answer might be helpful.

Community
  • 1
  • 1
android.dev
  • 26
  • 1
  • 7