Created
December 18, 2017 08:49
-
-
Save nguyenlinhnttu/f6fc41999556c395073ea5c714ca1d2e to your computer and use it in GitHub Desktop.
ConnectionReciever
This file contains 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 ConnectionReciever extends BroadcastReceiver { | |
public static ConnectionReceiverListener connectionReceiverListener; | |
public ConnectionReciever() { | |
super(); | |
} | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
ConnectivityManager cm = (ConnectivityManager) context | |
.getSystemService(Context.CONNECTIVITY_SERVICE); | |
NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); | |
if(activeNetwork != null) { | |
boolean isConnected = activeNetwork.isConnectedOrConnecting(); | |
if (connectionReceiverListener != null) { | |
connectionReceiverListener.onNetworkConnectionChanged(isConnected); | |
} | |
} else { | |
AppLog.log("activeNetwork Null"); | |
if (connectionReceiverListener != null) { | |
connectionReceiverListener.onNetworkConnectionChanged(false); | |
} | |
} | |
} | |
public static boolean isConnected() { | |
ConnectivityManager | |
cm = (ConnectivityManager) LilulaApp.getInstance().getApplicationContext() | |
.getSystemService(Context.CONNECTIVITY_SERVICE); | |
NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); | |
return activeNetwork != null | |
&& activeNetwork.isConnectedOrConnecting(); | |
} | |
public interface ConnectionReceiverListener { | |
void onNetworkConnectionChanged(boolean isConnected); | |
} | |
} |
This file contains 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 LilulaApp extends Application{ | |
private static LilulaApp mInstance; | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
mInstance = this; | |
TwitterConfig config = new TwitterConfig.Builder(this) | |
.logger(new DefaultLogger(Log.DEBUG)) | |
.twitterAuthConfig(new TwitterAuthConfig(getString(R.string.CONSUMER_KEY), getString(R.string.CONSUMER_SECRET))) | |
.debug(true) | |
.build(); | |
Twitter.initialize(config); | |
} | |
public static synchronized LilulaApp getInstance() { | |
return mInstance; | |
} | |
public void setConnectionListener(ConnectionReciever.ConnectionReceiverListener listener) { | |
ConnectionReciever.connectionReceiverListener = listener; | |
} | |
} |
This file contains 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
private ConnectionReciever mReceiver; | |
private IntentFilter mIntentFilter; | |
mIntentFilter = new IntentFilter(); | |
mIntentFilter.addAction(Constants.ACTION_CONNECTIVITY_CHANGE); | |
mIntentFilter.addAction(Constants.ACTION_WIFI_STATE_CHANGED); | |
mReceiver = new ConnectionReciever(); | |
registerReceiver(mReceiver, mIntentFilter); | |
@Override | |
protected void onPause() { | |
unregisterReceiver(mReceiver); | |
super.onPause(); | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
registerReceiver(mReceiver, mIntentFilter); | |
LilulaApp.getInstance().setConnectionListener(this); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment