0% found this document useful (0 votes)
12 views2 pages

Practical 21

The document provides instructions for creating two Android projects: MyApplication and MyBroadcastReceiver, demonstrating system broadcasts. It includes XML and Java code for both projects, detailing the setup of a button in MyApplication that sends a broadcast message and a receiver in MyBroadcastReceiver that listens for this message. Instructions for running the applications are also provided, emphasizing simultaneous execution in separate tabs.

Uploaded by

st5617067
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)
12 views2 pages

Practical 21

The document provides instructions for creating two Android projects: MyApplication and MyBroadcastReceiver, demonstrating system broadcasts. It includes XML and Java code for both projects, detailing the setup of a button in MyApplication that sends a broadcast message and a receiver in MyBroadcastReceiver that listens for this message. Instructions for running the applications are also provided, emphasizing simultaneous execution in separate tabs.

Uploaded by

st5617067
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/ 2

Practical 21

1. Write a program to demonstrate all system broadcast

Step 1: Create two different android projects namely i] MyApplication


ii] MyBroadcastReceiver

Project 1-MyApplication

XML CODE: JAVA CODE:


<?xml version="1.0" encoding="utf-8"?> package com.example.myapplication;
<RelativeLayout import android.app.Activity;
xmlns:android="http://schemas.android.com/apk/res/an import android.os.Bundle;
droid" import android.annotation.SuppressLint;
xmlns:app="http://schemas.android.com/apk/res- import android.content.Intent;
auto" import android.view.View;
xmlns:tools="http://schemas.android.com/tools" import android.widget.Button;
android:layout_width="match_parent" public class MainActivity extends Activity {
android:layout_height="match_parent" @SuppressLint("MissingInflatedId")
android:padding="32dp" @Override
tools:context=".MainActivity"> public void onCreate(Bundle savedInstanceState){
<Button super.onCreate(savedInstanceState);
android:id="@+id/button" setContentView(R.layout.activity_main);
android:layout_width="fill_parent" }
android:layout_height="wrap_content" public void onBroadcastsendbtnclicked(View v){
android:layout_alignParentTop="true" Intent intent=new Intent();
android:layout_marginTop="190dp"
android:onClick="onBroadcastsendbtnclicked" intent.setAction("com.example.MyBroadcastMessage");
android:text="Broadcast Intent" /> sendBroadcast(intent);
</RelativeLayout> }

Project 2 – MyBroadcastReceiver

MAIN_ACTIVITY JAVA CODE: MYBROADCASTRECEIVER JAVA CODE


package com.example.mybroadcastreceiver; package com.example.mybroadcastreceiver;
import androidx.appcompat.app.AppCompatActivity; import android.content.BroadcastReceiver;
import android.content.IntentFilter; import android.content.Context;
import android.os.Bundle; import android.content.Intent;
public class MainActivity extends AppCompatActivity { import android.util.Log;
@Override import android.widget.Toast;
protected void onCreate(Bundle savedInstanceState) { public class MyBroadcastReceiver extends BroadcastReceiver {
super.onCreate(savedInstanceState); @Override
setContentView(R.layout.activity_main); public void onReceive(Context context, Intent intent) {
IntentFilter intentFilter=new Log.i("Broadcast Receiver","Broadcast message is
IntentFilter("com.example.MyBroadcastMessage"); received");
MyBroadcastReceiver obj=new MyBroadcastReceiver(); Toast.makeText(context,"Broadcast Message is receive",
registerReceiver(obj,intentFilter); Toast.LENGTH_LONG).show();
}} }}
Practical 21

Androidmanifest.XML CODE

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


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyBroadcastReceiver"
tools:targetApi="31">
<receiver
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.MyBroadcastMessage">
</action>
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

INSTRUCTIONS:
STEP 1: First run the main application file.
STEP 2: Then run my broadcast receiver file.
STEP 3: Rerun the main application file.

NOTE:
RUN BOTH FILES IN SIMULTANEOUS TABS.

You might also like