简体   繁体   中英

Android Internet Connection checker OUTSIDE the app

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM