0% found this document useful (0 votes)
648 views

Practical No 21

The document describes a practical to develop a program to implement a broadcast receiver in Android. It explains what a broadcast receiver is, how to create and register one, lists some common system broadcast events, and provides sample code to demonstrate receiving various system broadcasts and sending a custom broadcast.

Uploaded by

sayedshaad02
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)
648 views

Practical No 21

The document describes a practical to develop a program to implement a broadcast receiver in Android. It explains what a broadcast receiver is, how to create and register one, lists some common system broadcast events, and provides sample code to demonstrate receiving various system broadcasts and sending a custom broadcast.

Uploaded by

sayedshaad02
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

Mobile Application Development (22617) Practical No.

21

Practical No. 21: Develop a program to implement broadcast receiver


I. Practical Significance
Broadcast Receivers simply respond to broadcast messages from other applications or from
the system. For example, applications can also initiate broadcasts to let other applications
know that some data has been downloaded to the device and is available for them to use, so
this is broadcast receiver who will intercept this communication and will initiate appropriate
action.

II. Relevant Program Outcomes (POs)


PO1- Basic knowledge
PO2- Discipline knowledge
PO3- Experiments and practice
PO4- Engineering tools

III. Competency and Practical Skills


“Create simple Android applications.”
This practical is expected to develop the following skills
1. Able to Create the Broadcast Receiver.
2. Able to Register Broadcast Receiver.

IV. Relevant Course Outcome(s)


1. Interpret features of Android operating system.
2. Configure Android environment and development tools.
3. Develop rich user Interfaces by using layouts and controls.

V. Practical Outcome (PrOs)


Develop a program to implement broadcast receiver.

VI. Relevant Affective Domain Related Outcome(s)


1. Work collaboratively in team
2. Follow ethical Practices.

VII. Minimum Theoretical Background Creating the


Broadcast Receiver:
A broadcast receiver is implemented as a subclass of Broadcast Receiver class and overriding
the onReceive() method where each message is received as an Intent object parameter.
Registering Broadcast Receiver:
An application listens for specific broadcast intents by registering a broadcast receiver in
AndroidManifest.xml file. Consider we are going to register MyReceiver for system generated
event ACTION_BOOT_COMPLETED which is fired by the system once the Android system has
completed the boot process.

Maharashtra State Board of Technical Education 1


Mobile Application Development (22617) Practical No. 21

VIII. Resources required (Additional)


Sr. Instrument /Object Specification Quantity Remarks
No.
Android enabled 2 GB RAM 1 Data cable is
1 smartphone / Android version mandatory for
supporting emulator emulators

IX. Practical related Questions


Note: Below given are few sample questions for reference. Teachers must design more
such questions to ensure the achievement of identified CO.
1. Differentiated between Activity Intent and Broadcasting Intent.
2. Draw Broadcast Receivers Lifecycle.
3. List the System Events related to Broadcast Receivers.

(Space for answers)


1.
Activity intent is used to start an activity, and is passed in startActivity() method.
Broadcasting intent is used to broadcast an intent (message), it is passed in sendBroadcast()
method.

2.
A broadcast receiver has single callback method:
void onReceive(Context curContext, Intent broadcastMsg)

Maharashtra State Board of Technical Education 2


Mobile Application Development (22617) Practical No. 21

3.
In android, several system events are defined as final static fields in the Intent class. Following
are the some of system events available in android applications.
Event Description
android.intent.action.BOOT_COMPLETED It raise an event, once boot completed.
android.intent.action.POWER_CONNECTED It is used to trigger an event when power
connected to the device.
android.intent.action.POWER_DISCONNECTED It is used to trigger an event when power
got disconnected from the device.
android.intent.action.BATTERY_LOW It is used to call an event when battery is
low on device.
android.intent.action.BATTERY_OKAY It is used to call an event when battery is
OKAY again.
android.intent.action.REBOOT It call an event when the device rebooted
again.

X. Exercise
Note: Faculty must ensure that every group of students use different input value.
(Use blank space for answers or attach more pages if needed)
1. Write a program to demonstrate all the system broadcast messages.

(Space for answers)


1.
//MainActivity.java

package com.jamiapolytechnic.brdcstrcvrex;

Maharashtra State Board of Technical Education 3


Mobile Application Development (22617) Practical No. 21

import android.app.Activity;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {


MyBroadcastReceiver receiver;
IntentFilter intentFilter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btnShow);
receiver = new MyBroadcastReceiver();
intentFilter = new IntentFilter("com.jamiapolytechnic.CUSTOM_ACTION");
intentFilter.addAction(Intent.ACTION_POWER_CONNECTED);
intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);
intentFilter.addAction(Intent.ACTION_HEADSET_PLUG);
intentFilter.addAction(Intent.ACTION_BATTERY_LOW);
intentFilter.addAction(Intent.ACTION_BATTERY_OKAY);
intentFilter.addAction(Intent.ACTION_BOOT_COMPLETED);

intentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
this.registerReceiver(receiver, intentFilter);
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
EditText st = (EditText)findViewById(R.id.txtMsg);
Intent intent = new Intent();

Maharashtra State Board of Technical Education 4


Mobile Application Development (22617) Practical No. 21

intent.setAction("com.jamiapolytechnic.CUSTOM_ACTION");
intent.putExtra("msg",(CharSequence)st.getText().toString());
sendBroadcast(intent);
}
});
}

@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
}

//=====================================================================
//MyBroadcastReceiver.java

package com.jamiapolytechnic.brdcstrcvrex;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.widget.Toast;

public class MyBroadcastReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent){
Toast.makeText(context,"Received Message:
"+intent.getAction().toString(),Toast.LENGTH_LONG).show();
}
}

//=====================================================================
//activity_main.xml

Maharashtra State Board of Technical Education 5


Mobile Application Development (22617) Practical No. 21

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/btnShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickShowBroadcast"
android:layout_marginLeft="130dp"
android:text="Show Broadcast"/>
</LinearLayout>

//=====================================================================
//AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jamiapolytechnic.brdcstrcvrex">

<application
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/Theme.BrdCstRcvrEx">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

Maharashtra State Board of Technical Education 6


Mobile Application Development (22617) Practical No. 21

</intent-filter>
</activity>
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="com.jamiapolytechnic.CUSTOM_ACTION" />
</intent-filter>
</receiver>
</application>

</manifest>

Maharashtra State Board of Technical Education 7


Mobile Application Development (22617) Practical No. 21

XI. References / Suggestions for further Reading


1. https://www.tutorialspoint.com/android
2. https://stuff.mit.edu
3. https://www.tutorialspoint.com/android/android_advanced_tutorial.pdf
4. https://developer.android.com

XII. Assessment Scheme

Performance indicators Weightage

Process related (10 Marks) 30%

1. Logic Formation 10%


2. Debugging ability 15%
3. Follow ethical practices 5%
Product related (15 Marks) 70%

4. Interactive GUI 20%


5. Answer to Practical related questions 20%
6. Expected Output 20%
7. Timely Submission 10%
Total (25 Marks) 100%

List of student Team Members


1
2
3
4

Dated signature of
Marks Obtained
Teacher
Process Product Total
Related(10) Related(15) (25)

Maharashtra State Board of Technical Education 8

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy