0% found this document useful (0 votes)
76 views4 pages

Mad 21

The document describes an Android programming exercise that broadcasts an intent when an airplane mode button is clicked. It includes code for an Activity with a button, a BroadcastReceiver to handle intents, and manifest files. The Activity registers the BroadcastReceiver and sends an intent on button click. The receiver checks for the airplane mode change intent and displays a toast message accordingly.

Uploaded by

Ganesh Ekambe
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)
76 views4 pages

Mad 21

The document describes an Android programming exercise that broadcasts an intent when an airplane mode button is clicked. It includes code for an Activity with a button, a BroadcastReceiver to handle intents, and manifest files. The Activity registers the BroadcastReceiver and sends an intent on button click. The receiver checks for the airplane mode change intent and displays a toast message accordingly.

Uploaded by

Ganesh Ekambe
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/ 4

Name: Ekambe Ganesh Dattatraya

Roll No. :53

Practical No. : 21

Exercise

ActivityMain.xml

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


<androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.android.com/a
pk/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">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="97dp"
android:layout_marginTop="341dp"
android:text="Broadcasting button"
app:layout_constraintStart_toStartOf="paren
t"
app:layout_constraintTop_toTopOf="paren
t" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.example.exp21;
import
androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import
android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import
android.widget.Button;

public class MainActivity extends AppCompatActivity


{Button button;
MyReceivermyReceiver = new
MyReceiver(); IntentFilter filter = new
IntentFilter();
@Override
protected void onCreate(Bundle savedInstanceState)
{super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{ MainActivity.this.registerReceiver(myReceiver,filter);
}

});

@Override
protected void onDestroy()
{ super.onDestroy();
MainActivity.this.
unregisterReceiver(myReceiver);
}
}

MyReceiver.java
package com.example.exp21;
import
android.content.BroadcastReceiver;
import android.content.Context;
import
android.content.Intent;
import
android.widget.Toast;

public class MyReceiver extends BroadcastReceiver


{public
MyReceiver(){
super();
}

@Override
public void onReceive(Context context, Intent intent)
{String intentAction =
intent.getAction(); if(intentAction !=
null){
String toastMessage = "unknown intent action";
switch(intentAction){
case
Intent.ACTION_AIRPLANE_MODE_CHANGED: boolean
status = intent.getBooleanExtra("state",true);
if(status){
toastMessage = "Airoplane Mode turned on";
}else {
toastMessage = "Airoplane Mode turned off";
}

break;
}

Toast.makeText(context,toastMessage,Toast.LENGTH_SHORT).show();
}

}
}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcast_messages">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launch
er"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Exp21">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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


</intent-filter>
</activity>
<receiver
android:name=".MyReceiver"
android:exported="false">
</receiver>

</application>

</manifest>
Mobile Application Development(22617)

Output

You might also like