Unit - 07 Understanding Mobile Networking Fundamentals

Lesson Plan: Unit - 07
Subject: P15A2AAD - Android Application Development
Topic of Study: Understanding Mobile Networking Fundamentals
Grade/Level: Master of Computer Applications
Objective: Understanding Mobile Networking Fundamentals
Time Allotment: 55 Minutes


  • Understanding Mobile Networking Fundamentals
    • Why Application need network…
      • Deliver fresh and updated content
      • To enable social network features
      • To offload heavy processing
      • To enable data storage beyond on the device
    • To work with java networking by java.net package.
    • To developed network based application – you must take care.
  • Required Permissions:
    • To perform network operations in Android, your application must include certain permissions. 
    • Open manifests/AndroidManifest.xml, and add the following permissions before the application tag:
    • The ACCESS_NETWORK_STATE permission is required for your application to check the network state of the device, while the INTERNET permission is required for your application to access the Internet.
  • How to check internet connection in android?
  • Step 01: Manifest file
   package = "com.example.andy.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">
     
         
           
           
         
     
   
  • Step 02: MainActivity.java
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
   private BroadcastReceiver MyReceiver = null;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      MyReceiver = new MyReceiver();
      TextView click=findViewById(R.id.click);
      click.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            broadcastIntent();
         }
      });
   }
   public void broadcastIntent() {
      registerReceiver(MyReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
   }
   @Override
   protected void onPause() {
      super.onPause();
      unregisterReceiver(MyReceiver);
   }
}
  • Step 03:Create a NetworkUtil.java
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
class NetworkUtil {
   public static String getConnectivityStatusString(Context context) {
      String status = null;
      ConnectivityManager cm = (ConnectivityManager)           context.getSystemService(Context.CONNECTIVITY_SERVICE);
     NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
      if (activeNetwork != null) {
         if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
            status = "Wifi enabled";
            return status;
         } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
            status = "Mobile data enabled";
            return status;
         }
      } else {
    status = "No internet is available";
      return status;
    }
    return status;
   }
}
  • Step 04:Create a broadcast receiver class and named as MyReceiver.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
   @Override
   public void onReceive(Context context, Intent intent) {
      String status = NetworkUtil.getConnectivityStatusString(context);
      if(status.isEmpty()) {
         status="No Internet Connection";
      }
      Toast.makeText(context, status, Toast.LENGTH_LONG).show();
   }
}

  • Step 05: Update your broadcast receiver in manifest file
   package = "com.example.andy.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">
        
         
         
     
   
     
         
           
           
         
     
   
  • Step 06: res/layout/activity_main.xml
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   tools:context = ".MainActivity">
   android:id="@+id/click"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Click here"
   app:layout_constraintBottom_toBottomOf="parent"
   app:layout_constraintLeft_toLeftOf="parent"
   app:layout_constraintRight_toRightOf="parent"
   app:layout_constraintTop_toTopOf="parent" />

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form