Skip to main content

Check Network Using BroadCastReceiver

Hello friends,

Hope you already see this example in lot of websites and blogs. I'm also doing these, because hope my example might be easy and understandable.

Let's go.

Create the class:

ConnectivityReceiver.Java

public class ConnectivityReceiver extends BroadcastReceiver {

    public static ConnectivityReceiverListener connectivityReceiverListener;

    public ConnectivityReceiver() {
        super();
    }

    @Override
    public void onReceive(Context context, Intent arg1) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        boolean isConnected = activeNetwork != null
                && activeNetwork.isConnectedOrConnecting();

        if (connectivityReceiverListener != null) {
            connectivityReceiverListener.onNetworkConnectionChanged(isConnected);
        }
    }

    public static boolean isConnected() {
        ConnectivityManager
                cm = (ConnectivityManager) ShukranApplication.getInstance().getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        return activeNetwork != null
                && activeNetwork.isConnectedOrConnecting();
    }

    public interface ConnectivityReceiverListener {
        void onNetworkConnectionChanged(boolean isConnected);
    }
}

In AndroidManifest.xml

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />

then don't forget to add the receiver class.
    <receiver
        android:name=".service.ConnectivityReceiver"
        android:enabled="true">
        <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>

Then In your application class,

public class MyApplication extends Application{
    private static MyApplication mInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
    }

    public static synchronized MyApplication getInstance() {
        return mInstance;
    }

    public void setConnectivityListener(ConnectivityReceiver.ConnectivityReceiverListener listener) {
        ConnectivityReceiver.connectivityReceiverListener = listener;
    }
}


This will be the final step, In anyother activity or fragment, Just call like that.

public class MyActivity extends AppCompatActivity
        implements ConnectivityReceiver.ConnectivityReceiverListener{
@Override
    public void onNetworkConnectionChanged(boolean isConnected) {
        showSnack(isConnected);
    }

}

In events or directly to call,

If(ConnectivityReceiver.isConnected()){
//if true internet connected
}

Comments

Popular posts from this blog

Clear the app data programmatically in android

Clear the App Data Programmatically in Android

Application data has been created due to use shared preference data, databases and network caches data. This data has been manually clear on Settings -- > Apps (or) Application Manager --> Select the app you want to clear the data. --> Then click button clear data to erase the app from the Phone and SDCARD.

Applications like facebook, google+, gmail and some games captures more data on phone and SDCARD.

Once you clear the data of your app, all passwords and saved settings in app has been lost. So carefull to use this method.

Create the Class MyApplication

public class MyApplication extends Application {
 private static MyApplication instance;
 @Override
 public void onCreate() {
  super.onCreate();
  instance = this;
 }
 public static MyApplication getInstance(){
  return instance;
 }
 public void clearApplicationData() {
  File cache = getCacheDir();
  File appDir = new File(cache.getParent());
  if(appDir.exists()){
   String[] children = app…

convert an integer to a string in java

Hi, have a great day. Hope It's like to be homework question or to be interview question. Yes, it's right. In future posts, I'm gonna be post a interview questions and answers, algorithms and some critical problems.

Requirement:
1) return an empty string if value is negative.
2) return an empty string if the conversion fails.
3) return the proper string of the value using characer '0' to '9' and 'A' to 'F'
for example:
MyIntToString(254, 16) return "FE"
MyIntToString(254,  8) return "376"
MyIntToString(254,  2) return "11111110"


Rules:
1) You SHALL NOT import any Java libraries for the implementation.
2) You are allowed to using the string class related functions, like: insert(),
appened, trim()and etc for the implementation
3) You SHALL NOT invoke static functions from java.lang.Integer class to perform the conversion.
4) Your code style is graded, hence it is encouraged to structure the code professionally
and equip…

Print the large string values in android log cat.

Hi,

Have a happy Monday.

In Android, sometimes we need to print the large numbers of strings value in log. But log can't accept whole value of the string.

In this case, we need to split the logs. It's very simple android code, I attached below this blog.

public static void logLargeString(String TAG, String msg) {         if (str.length() > 3000) {             Log.i(TAG, msg.substring(0, 3000));             logLargeString(TAG, msg.substring(3000));         } else {             Log.i(TAG, msg);         } }
Just use it and enjoy...