Skip to content

Instantly share code, notes, and snippets.

@code4lifevn
Created January 6, 2015 03:46
Show Gist options
  • Save code4lifevn/00d4083e70cc9d63c9b7 to your computer and use it in GitHub Desktop.
Save code4lifevn/00d4083e70cc9d63c9b7 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
<activity android:name="MyActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver android:name=".NetworkReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
</application>
</manifest>
package com.example.test;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
/**
* Created by SexyBoyHaha on 1/6/2015.
*/
public class NetworkReceiver extends BroadcastReceiver {
private static boolean isRunning = true;
@Override
public void onReceive(Context context, Intent intent) {
final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetInfo != null) {
if(isRunning) {
System.out.println("FAP");
// do subroutines here
isRunning = false;
}
}
else {
isRunning = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment