1. While using Broadcast Receiver it will always trigger when internet is not there if the activity is closed. So for this we have to make an java class naming MyApplication.java extended by Application .This class is used to check if activity is visible or not and trigger receiver if activity is visible. The class will look like following:-
public class MyApplication extends Application { private static MyApplication myApplication; @Override public void onCreate() { super.onCreate(); Log.d("supriya","onCreate"); myApplication = this; } public static synchronized MyApplication getInstance() { Log.d("supriya","synchronized MyApplication"); return myApplication; } public void setConnectivityListener(ConnectivityReceiver
.ConnectivityReceiverListener listener) { Log.d("supriya","setConnectivityListener "); ConnectivityReceiver.connectivityReceiverListener = listener; } }
2. Create BroadcastReceiver :-
public class ConnectivityReceiver extends BroadcastReceiver { public static ConnectivityReceiverListener connectivityReceiverListener; public ConnectivityReceiver() { super(); } @Override public void onReceive(Context context, Intent intent) { if(connectivityReceiverListener != null) { connectivityReceiverListener.onNetworkConnectionChanged(amIConnected(context)); } } public static boolean amIConnected(Context context) { ConnectivityManager connectivityManager = (ConnectivityManager)
context.getApplicationContext().getSystemService(context.CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); return activeNetworkInfo != null && activeNetworkInfo.isConnected(); } public interface ConnectivityReceiverListener { void onNetworkConnectionChanged(boolean isConnected); } }
3.Manifests will be like following :-
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.INTERNET" /> <application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".ConnectivityReceiver"
android:enabled="true"> <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> </receiver> </application>
4. MainActivity.java:-
public class MainActivity extends AppCompatActivity implements ConnectivityReceiver.ConnectivityReceiverListener{ TextView tvMsg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvMsg= (TextView) findViewById(R.id.tvMsg); checkConnection(); ConnectivityReceiver.amIConnected(this); } private void checkConnection() { boolean isConnected = ConnectivityReceiver.amIConnected(this); if(isConnected) { tvMsg.setText("Connected"); } else { tvMsg.setText("No internet connection"); } } @Override protected void onResume() { super.onResume(); // register connection status listener
MyApplication.getInstance().setConnectivityListener(this); } /** * Callback will be triggered when there is change in
* network connection */
public void onNetworkConnectionChanged(boolean isConnected) {
if(isConnected) {
tvMsg.setText("Connected");
Toast.makeText(getApplicationContext(), "isConnected==" + isConnected, Toast.LENGTH_LONG).show();
} else {
tvMsg.setText("No internet connection");
Toast.makeText(getApplicationContext(), "notConnected==" + isConnected, Toast.LENGTH_LONG).show();
}
}
}
No comments:
Post a Comment