Created
November 10, 2015 16:55
-
-
Save SMontiel/b45e29aae3df69254c34 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="your.package.name" > | |
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | |
<uses-permission android:name="android.permission.INTERNET" /> | |
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> | |
<application | |
android:allowBackup="true" | |
android:icon="@drawable/ic_launcher" | |
android:label="@string/app_name" | |
android:name="your.package.name.MyApp" | |
android:theme="@style/AppTheme" > | |
<receiver | |
android:enabled="true" | |
android:exported="true" | |
android:label="BR" | |
android:name="your.package.name.main.NetworkStateReceiver" > | |
<intent-filter> | |
<action android:name="android.intent.action.BOOT_COMPLETED" > </action> | |
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> | |
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" /> | |
</intent-filter> | |
</receiver> | |
<activity | |
android:name="your.package.name.main.MainActivity" | |
android:label="@string/app_name" | |
android:theme="@style/AppTheme.BrandedLaunch" > | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.smontiel.strap.support.v7.AppCompatActivity; | |
import android.os.Bundle; | |
//import de.greenrobot.event.EventBus; | |
import android.widget.Toast; | |
import com.squareup.otto.*; | |
import android.content.*; | |
import android.net.*; | |
public class MainActivity extends AppCompatActivity | |
{ | |
private String TAG = MainActivity.class.getSimpleName(); | |
Bus bus =MyApp.getBus(); | |
@Override protected void onPreInject() | |
{ setContentView(R.layout.main); } | |
NetworkStateReceiver mReceiver; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) | |
{ | |
setTheme(R.style.AppTheme); | |
super.onCreate(savedInstanceState); | |
bus.post(new NetworkStateChanged(false) ); | |
//EventBus.getDefault().register(this); | |
// register EventBus | |
} | |
@Override | |
protected void onStart() | |
{ | |
super.onStart(); | |
//bus.register(this); | |
} | |
@Override | |
protected void onResume() | |
{ | |
this.mReceiver = new NetworkStateReceiver(); | |
registerReceiver(this.mReceiver, | |
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); | |
super.onResume(); | |
bus.register(this); | |
} | |
@Override | |
protected void onPause() | |
{ | |
unregisterReceiver(mReceiver); | |
super.onPause(); | |
bus.unregister(this); | |
} | |
@Override | |
protected void onDestroy() | |
{ super.onDestroy(); | |
//bus.unregister(this); | |
//EventBus.getDefault().unregister(this); | |
// unregister EventBus | |
} | |
// method that will be called when someone posts an event NetworkStateChanged | |
@Subscribe | |
public void onEventMainThread(NetworkStateChanged event) | |
{ | |
if (!event.isInternetConnected()) | |
{ | |
Toast.makeText(this, "No Internet connection!", Toast.LENGTH_SHORT).show(); | |
}else{Toast.makeText(this, " Internet connection!", Toast.LENGTH_SHORT).show();} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.squareup.otto.*; | |
import android.app.*; | |
public class MyApp extends Application | |
{ | |
private static Bus mEventBus; | |
public static Bus getBus() { | |
return mEventBus; | |
} | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
mEventBus = new Bus(ThreadEnforcer.ANY); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NetworkStateChanged | |
{ | |
public boolean mIsInternetConnected; | |
public NetworkStateChanged(boolean isInternetConnected) | |
{ | |
this.mIsInternetConnected = isInternetConnected; | |
} | |
public boolean isInternetConnected() | |
{ | |
return this.mIsInternetConnected; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.net.ConnectivityManager; | |
import android.net.NetworkInfo; | |
import android.widget.*; | |
//import de.greenrobot.event.EventBus; | |
public class NetworkStateReceiver extends BroadcastReceiver | |
{ | |
// post event if there is no Internet connection | |
public void onReceive(Context context, Intent intent) | |
{ | |
//super.onReceive(context, intent); | |
if(intent.getExtras()!=null) | |
{ | |
NetworkInfo ni=(NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO); | |
if(ni!=null && ni.getState()==NetworkInfo.State.CONNECTED) | |
{ | |
//Toast.makeText(context, "Si hay",1).show(); | |
MyApp.getBus().post(new NetworkStateChanged(true) ); | |
// there is Internet connection | |
} else | |
MyApp.getBus().post(new NetworkStateChanged(false) ); | |
if(intent .getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,Boolean.FALSE)) | |
{ | |
// no Internet connection, send network state changed | |
MyApp.getBus().post(new NetworkStateChanged(false) ); | |
//EventBus.getDefault().post(new NetworkStateChanged(false)); | |
} | |
} | |
} | |
} |
do you think it will consume more power battery of mobile?
All you need is a receiver declaration and intent filter. Why would you subject yourself to the rest of the spaghetti?
how to use in Fragment?
How do you stop it when the Activity changes?
Using canary it shows this as a memory leak.
android.net.conn.CONNECTIVITY_CHANGE
This is deprecated for Android N and higher
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, Thanks for your gist.
But in line 28 NetworkStateReceiver.java, why do you call post event again? As i read, the event already post in line 24.