0% found this document useful (0 votes)
90 views3 pages

Practical No 8 (C)

This document provides instructions for a program that uses services, notifications, and broadcast receivers in Android. It contains 4 steps: 1) Create a new project. 2) Configure the activity_main.xml layout file. 3) Add code to the MainActivity class to register a broadcast receiver for airplane mode changes. 4) Create a new AirplaneModeChangeReceiver class that extends BroadcastReceiver to detect airplane mode changes and display a toast notification.

Uploaded by

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

Practical No 8 (C)

This document provides instructions for a program that uses services, notifications, and broadcast receivers in Android. It contains 4 steps: 1) Create a new project. 2) Configure the activity_main.xml layout file. 3) Add code to the MainActivity class to register a broadcast receiver for airplane mode changes. 4) Create a new AirplaneModeChangeReceiver class that extends BroadcastReceiver to detect airplane mode changes and display a toast notification.

Uploaded by

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

Name:suyash munde Roll no.

47

Practical no 8(C)

Programs on Services, notification and broadcast receivers

Step 1: Create a New Project


Step 2: Working with the activity_main.xml file
Go to the activity_main.xml file and refer to the following code. Below is the code for
the activity_main.xml file.

<?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">
</androidx.constraintlayout.widget.ConstraintLayout>

Step 3: Working with the MainActivity file 


Go to the MainActivity file and refer to the following code. Below is the code for
the MainActivity file. Comments are added inside the code to understand the code in more
detail. 

package com.example.prac_8c;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

AirplaneModeChangeReceiver airplaneModeChangeReceiver = new


AirplaneModeChangeReceiver();

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

1
Name:suyash munde Roll no.47

@Override
protected void onStart() {
super.onStart();
IntentFilter filter = new
IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
registerReceiver(airplaneModeChangeReceiver, filter);
}

@Override
protected void onStop() {
super.onStop();
unregisterReceiver(airplaneModeChangeReceiver);
}
}

Step 4: Create a new class 


Go to app > java > your package name(in which the MainActicity is present) >
right-click > New > Java File/Class and name the files
as AirplaneModeChangeReceiver. Below is the code for
the AirplaneModeChangeReceiver file. Comments are added inside the code to
understand the code in more detail. 

package com.example.prac_8c;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.widget.Toast;

public class AirplaneModeChangeReceiver extends BroadcastReceiver


{

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

if (isAirplaneModeOn(context.getApplicationContext())) {
Toast.makeText(context, "AirPlane mode is on",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText (context, "AirPlane mode is off",
Toast.LENGTH_SHORT).show();
}
}
private static boolean isAirplaneModeOn(Context context) {

2
Name:suyash munde Roll no.47

return
Settings.System.getInt(context.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
}
}

output:

You might also like