[go: up one dir, main page]

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

All System Broadcast

The document contains an Android manifest file and code for a broadcast receiver application. The manifest defines the app's package, application settings, and registers a broadcast receiver for actions related to airplane mode and power connection. The MainActivity sends a broadcast intent to the receiver, which displays toast messages based on the received actions.
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)
17 views3 pages

All System Broadcast

The document contains an Android manifest file and code for a broadcast receiver application. The manifest defines the app's package, application settings, and registers a broadcast receiver for actions related to airplane mode and power connection. The MainActivity sends a broadcast intent to the receiver, which displays toast messages based on the received actions.
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

Manifest File:

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.example.brodcastreceiver">

<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/AppTheme">

<receiver android:name=".broadcastReceiver">

<intent-filter>

<action android:name="android.intent.action.AIRPLANE_MODE" />

<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />

<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>

</intent-filter>

</receiver>

<activity android:name=".MainActivity">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

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

</intent-filter>

</activity>

</application>

</manifest>

MainActivity Code:

package com.example.brodcastreceiver;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;

import android.os.Bundle;
import android.view.View;

import android.widget.Button;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Intent i=new Intent(getApplicationContext(),broadcastReceiver.class);

sendBroadcast(i);

BroadcastReceiver Code:

package com.example.brodcastreceiver;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.widget.Toast;

public class broadcastReceiver extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

if(intent.getAction()==Intent.ACTION_AIRPLANE_MODE_CHANGED){

Toast.makeText(context,"Airplane Mode Changed",Toast.LENGTH_SHORT).show();

if(intent.getAction()==Intent.ACTION_POWER_CONNECTED){

Toast.makeText(context,"Charging",Toast.LENGTH_SHORT).show();

if(intent.getAction()==Intent.ACTION_POWER_DISCONNECTED){

Toast.makeText(context,"Charger Removed",Toast.LENGTH_SHORT).show();

}
}

You might also like