1

My app needs to do something whenever the internet is connected and vice-versa. However, I need to do this OUTSIDE my application. There are a lot of ideas and help here on stackoverflow but it is always WITHIN the application. I know that the AlarmManager works outside the application but it would just be draining the battery if I try to check if there's internet connection every 5 minutes. What I wanted is to check the connection outside the app to download something.

I also found out that Intent Service can work outside the application as well. I have tried to put this inside the Wakeful Broadcast Receiver. However, it is still not being called.

Can someone out there help me? Thanks!

Here's my Wakeful Broadcast Receiver

public class ConnectivityOutsideAppReceiver extends WakefulBroadcastReceiver {

    private static final String LOG_TAG = ConnectivityOutsideAppReceiver.class.getSimpleName();

    private ConnectivityManager connectivityManager;
    private static boolean connection = false;

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

        ComponentName comp = new ComponentName(context.getPackageName(),
                ConnectivityOutsideAppService.class.getName());

        // Start the service, keeping the device awake while it is
        // launching.
        startWakefulService(context, (intent.setComponent(comp)

        ));
    }

}

This is the Intent Service

public class ConnectivityOutsideAppService extends IntentService {

private static final String LOG_TAG = ConnectivityOutsideAppService.class.getSimpleName();
private ConnectivityManager connectivityManager;
private static boolean connection = false;
private Context context;

public ConnectivityOutsideAppService() {
   super(ConnectivityOutsideAppService.class.getSimpleName());
    this.context = context;
}

@Override
protected void onHandleIntent(Intent intent) {
    connectivityManager =
            (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    checkConnectionOnDemand();

    Log.e(LOG_TAG, " ## onHandleIntent##");
    if (connection == true
            && intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,
            false)) {
        Log.e(LOG_TAG, " ## connection == true ##");
        connection = false;
    } else if (connection == false
            && !intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,
            false)) {
        Log.e(LOG_TAG, " ## connection == false ##");
        connection = true;
    }

    ConnectivityOutsideAppReceiver.completeWakefulIntent(intent);
}

public static boolean hasConnection() {
    return connection;
}

private void checkConnectionOnDemand() {
    Log.e(LOG_TAG, " ## checkConnectionOnDemand ##");
    final NetworkInfo info = connectivityManager.getActiveNetworkInfo();
    if (info == null || info.getState() != NetworkInfo.State.CONNECTED) {
        if (connection == true) {
            Log.e(LOG_TAG, " ## connection == true ##");
            connection = false;
        }
    } else {
        if (connection == false) {
            Log.e(LOG_TAG, " ## connection == false ##");
            connection = true;
        }
    }
}
}

This is my Android Manifest

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

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

    <service
        android:name="com.timetrackermobilelog.BusinessServices.ConnectivityOutsideAppService"
        android:exported="false"/>

    <receiver android:name="com.timetrackermobilelog.Utilities.ConnectivityOutsideAppService"
              android:enabled="true"
              android:process=":remote">

        <intent-filter android:priority="1000" >
            <action android:name="com.timetrackermobilelog.Utilities.ConnectivityOutsideAppService" />
            <category android:name="com.Utilities" />
        </intent-filter>
    </receiver>

I have tried registering the receiver inside the Activity but it doesn't work as well.

IntentFilter intentFilter = new IntentFilter("com.pointwest.timetrackermobilelog.Utilities.ConnectivityOutsideAppReceiver");
ConnectivityOutsideAppReceiver connectivityOutsideAppReceiver = new ConnectivityOutsideAppReceiver();
registerReceiver(connectivityOutsideAppReceiver, intentFilter);

Is there anything that I missed?

Paula Kristin
  • 813
  • 2
  • 11
  • 27
  • 1
    Register your Receiver in the manifest for the [`CONNECTIVITY_CHANGE` action](http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html#MonitorChanges), instead of your custom action. – Mike M. Apr 11 '16 at 04:15
  • have you debug your code..? where should you get problem..? – Vishal Thakkar Apr 11 '16 at 04:18
  • Hi @MikeM. I'll that one out and get back to you. – Paula Kristin Apr 11 '16 at 04:54
  • @VishalHalani. I already did and there's no error. ConnectivityOutsideAppService isn't called. – Paula Kristin Apr 11 '16 at 04:55
  • OK. Btw, make sure to remove the `` from the ``, too. – Mike M. Apr 11 '16 at 04:55
  • @MikeM. It called the ConnectivityOutsideAppService. However it threw an error. `java.lang.NullPointerException at com.timetrackermobilelog.BusinessServices.ConnectivityOutsideAppService.onHandleIntent(ConnectivityOutsideAppService.java:30)`. That is this line : `connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);` – Paula Kristin Apr 11 '16 at 05:06
  • The `Context` is null when you assign it to your field in the constructor. Get rid of `context`, and just call `getSystemService()`. There's no point in keeping a `Context` field in a `Service` anyway. – Mike M. Apr 11 '16 at 05:09
  • 1
    @MikeM. Thanks! It worked! I will write the answer here. Kudos to you! – Paula Kristin Apr 11 '16 at 05:17

1 Answers1

2

Thanks to @Mike M. in giving the answer with just a few lines of changes.

In Android Manifest, change it to :

<intent-filter android:priority="1000" >
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
            </intent-filter>

And onHandleIntent of the Service, change it to :

connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
Paula Kristin
  • 813
  • 2
  • 11
  • 27