0% found this document useful (0 votes)
68 views8 pages

Practical 21

The document describes an experiment to implement a broadcast receiver in Android. It includes code for a MainActivity class that registers the broadcast receiver at startup and unregister it at destroy. It also includes code for a ConnectReceiver class that extends BroadcastReceiver and handles connectivity change intents by checking the network state and displaying toast messages. The AndroidManifest file is updated to declare the broadcast receiver and permissions.

Uploaded by

Rehan Pathan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views8 pages

Practical 21

The document describes an experiment to implement a broadcast receiver in Android. It includes code for a MainActivity class that registers the broadcast receiver at startup and unregister it at destroy. It also includes code for a ConnectReceiver class that extends BroadcastReceiver and handles connectivity change intents by checking the network state and displaying toast messages. The AndroidManifest file is updated to declare the broadcast receiver and permissions.

Uploaded by

Rehan Pathan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

MAD Experiment 21

Aim: Develop a program to implement

broadcast receiver.

X. Exercise:

1. Write a program to demonstrate all the system

broadcast messages.

MainActivity.java

package com.example.expt21;
import androidx.appcompat.app.AppCompatActivity;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;

public class MainActivity extends

AppCompatActivity { ConnectReceiver

receiver;

IntentFilter intentfilter;
@Override
protected void onCreate(Bundle

savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
receiver= new ConnectReceiver();
}

@Override
protected void onStart() {
super.onStart();
intentfilter= new
IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(receiver,intentfilter);
}

@Override
protected void
onDestroy() {
super.onDestroy();
unregisterReceiver(rece
iver);
}
}
ConnectReceiver,java
package com.example.expt21;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import

android.widget.Toast;

import

java.net.NetworkInterface;

public class ConnectReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {

Log.d("Status","msg"+intent.getAction());

if(intent.getAction().equals("android.net.conn.CONNECTIVITY_C

HANGE"))
{
ConnectivityManager

c1=(ConnectivityManager)context.getSystemService(Context.CONNECTIVIT

Y_SERVICE);

NetworkInfo ni=c1.getActiveNetworkInfo();
if(ni!=null && ni.isConnectedOrConnecting())
{
Toast.makeText(context,"N/W is
connected",Toast.LENGTH_LONG).show();
if(ni.getType()==ConnectivityManager.TYPE_WIFI)
{
Toast.makeText(context,"Its WIFI",Toast.LENGTH_LONG).show();
}
if(ni.getType()==ConnectivityManager.TYPE_MOBILE)
{
Toast.makeText(context,"Its Mobile
Data",Toast.LENGTH_LONG).show();
}
}
else
Toast.makeText(context,"N/W is not
Connected",Toast.LENGTH_LONG).show();
}
}
}
AndroidManifest.xml
Add following tags in AnroidManifest.xml.

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

<receiver android:name=".ConnectReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE">
</action>
</intent-filter>
</receiver>
</application>
</manifest>

You might also like