Mobile Application Development Practical 21
Question 1
Write a program to demonstrate all the system broadcast messages.
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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
AndroidManifest.xml
<?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:supportsRtl="true"
android:theme="@style/Theme.Practical21"
tools:targetApi="31">
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE"></action>
<action android:name="android.intent.action.BATTERY_LOW"></action>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"></action>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"></action>
<action android:name="android.intent.action.BATTERY_OKAY"></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>
MyReceiver.java
package com.example.practical21;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
if(intent.getAction()=="android.intent.action.AIRPLANE_MODE")
Toast.makeText(context,"Airplane mode",Toast.LENGTH_LONG).show();
if(intent.getAction()=="android.intent.action.BATTERY_LOW")
Toast.makeText(context,"Battery Low",Toast.LENGTH_LONG).show();
if(intent.getAction()=="android.intent.action.BATTERY_OKAY")
Toast.makeText(context,"Battery okay",Toast.LENGTH_LONG).show();
if(intent.getAction()=="android.intent.action.ACTION_POWER_CONNECTED")
Toast.makeText(context,"Charging on",Toast.LENGTH_LONG).show();
if(intent.getAction()=="android.intent.action.ACTION_POWER_DISCONNECTED")
Toast.makeText(context,"Charger disconnected",Toast.LENGTH_LONG).show();
}
}
MainActivity.java
package com.example.practical18;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
EditText ed;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed=findViewById(R.id.editText);
b=findViewById(R.id.button);
View.OnClickListener cl=new View.OnClickListener() {
@Override
public void onClick(View view) {
String url=ed.getText().toString();
Intent i=new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
};
b.setOnClickListener(cl);
}
}
Output



