tracman-android/app/src/main/java/us/keithirwin/tracman/ConnectionReceiver.java

36 lines
983 B
Java
Raw Normal View History

2017-04-26 10:30:01 -06:00
package us.keithirwin.tracman;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
2017-04-26 12:30:04 -06:00
//import android.util.Log;
2017-04-26 10:30:01 -06:00
public class ConnectionReceiver extends BroadcastReceiver {
2017-04-26 12:30:04 -06:00
//private static final String TAG = "ConnectionReceiver";
2017-04-26 10:30:01 -06:00
@Override
public void onReceive(Context context, Intent intent) {
2017-04-26 10:57:45 -06:00
//Log.d(TAG,"onReceive() called");
2017-04-26 10:30:01 -06:00
// Get connection information
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
// Prepare intent
Intent locationServiceIntent = new Intent(context, LocationService.class);
// Check connection
if (networkInfo!=null) {
2017-04-26 10:57:45 -06:00
//Log.d(TAG, "Connected");
2017-04-26 10:30:01 -06:00
context.startService(locationServiceIntent);
}
else {
2017-04-26 10:57:45 -06:00
//Log.d(TAG,"Disconnected");
2017-04-26 10:30:01 -06:00
context.stopService(locationServiceIntent);
}
}
}