Зарегистрируйте трансляцию в манифесте и используйте код ниже.
public class NetworkReceiver extends BroadcastReceiver {
Cursor cursor;
SQLHelper helper;
Boolean IsSubmitted=false;
Context c;
@Override
public void onReceive(Context context, Intent intent) {
helper = new SQLHelper(context);
this.c = context;
pt = new ProcessTask();
if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.getDetailedState() == NetworkInfo.DetailedState.CONNECTED) {
Log.d("Network", "Internet YAY");
// Code when internet is connected
cursor = helper.getProcessTask(context);
getdataFromSql(cursor,context);
} else if (networkInfo != null && networkInfo.getDetailedState() == NetworkInfo.DetailedState.DISCONNECTED) {
Log.d("Network", "No internet :(");
}
}
}
в манифесте добавить
<receiver
android:name=".NetworkReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>