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

Practical No 21

This document contains code for an Android application that registers a broadcast receiver. The MainActivity registers the DemoReceiver to listen for changes to airplane mode, timezone, battery level, and date. When one of these system broadcasts is received, the DemoReceiver will display a Toast with the action name of the intent. The application demonstrates how to register and unregister a broadcast receiver within the activity lifecycle of an Android application.

Uploaded by

atharvabutte03
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)
42 views2 pages

Practical No 21

This document contains code for an Android application that registers a broadcast receiver. The MainActivity registers the DemoReceiver to listen for changes to airplane mode, timezone, battery level, and date. When one of these system broadcasts is received, the DemoReceiver will display a Toast with the action name of the intent. The application demonstrates how to register and unregister a broadcast receiver within the activity lifecycle of an Android application.

Uploaded by

atharvabutte03
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 No 21

Q1.

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
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">

<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="162dp"
android:layout_height="48dp"
android:ems="10"
android:inputType="textPersonName"
android:singleLine="false"
android:text="Atharva Butte"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java
package com.example.practical21;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {


DemoReceiver receiver = new DemoReceiver();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
protected void onStart() {
super.onStart();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
filter.addAction(Intent.ACTION_BATTERY_LOW);
filter.addAction(Intent.ACTION_DATE_CHANGED);
this.registerReceiver(receiver,filter);
}

@Override
protected void onStop() {
super.onStop();
this.unregisterReceiver(receiver);
}
}
DemoReceiver.java
package com.example.practical21;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class DemoReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,intent.getAction(),Toast.LENGTH_LONG).show();
}
}

You might also like