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

Addaction Lincoln Broadcast Example

The document contains code for an Android application that demonstrates the use of a BroadcastReceiver to listen for various system broadcasts such as boot completion, battery status changes, and power connection events. It includes the layout file 'activity_main.xml' and the main activity 'MainActivity.java' where the receiver is registered and unregistered. Additionally, the 'AndroidManifest.xml' specifies the necessary permissions for receiving these broadcasts.

Uploaded by

arqamqazi549
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)
29 views3 pages

Addaction Lincoln Broadcast Example

The document contains code for an Android application that demonstrates the use of a BroadcastReceiver to listen for various system broadcasts such as boot completion, battery status changes, and power connection events. It includes the layout file 'activity_main.xml' and the main activity 'MainActivity.java' where the receiver is registered and unregistered. Additionally, the 'AndroidManifest.xml' specifies the necessary permissions for receiving these broadcasts.

Uploaded by

arqamqazi549
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

Practical No.

21
Code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> [Link](savedInstanceState);
<LinearLayout
xmlns:android="[Link] setContentView([Link].activity_main);
m/apk/res/android"
// getActionBar().setTitle("BroadCast
xmlns:tools="[Link] Example");
ools"
android:layout_width="match_parent" receiver = new
android:layout_height="match_parent" SystemBroadcastReciever();
android:orientation="vertical" IntentFilter filter = new IntentFilter();
android:gravity="center" // Adding multiple system broadcasts
tools:context=".MainActivity">
<TextView [Link](Intent.ACTION_BOOT_
COMPLETED);
android:layout_width="wrap_content"
[Link](Intent.ACTION_BATTE
android:layout_height="wrap_content" RY_LOW);
android:text="This is My
Application" [Link](Intent.ACTION_BATTE
/> RY_OKAY);
</LinearLayout>
[Link](Intent.ACTION_POWER
[Link] _CONNECTED);
package [Link];
[Link](Intent.ACTION_POWER
import [Link]; _DISCONNECTED);

import [Link](Intent.ACTION_SCREE
[Link] N_ON);
ity;
import [Link]; [Link](Intent.ACTION_SCREE
import [Link]; N_OFF);
import [Link];
[Link](Intent.ACTION_AIRPLA
NE_MODE_CHANGED);
public class MainActivity extends
AppCompatActivity {
[Link](Intent.ACTION_USER_P
private SystemBroadcastReciever
RESENT);
receiver;
@Override
[Link](Intent.ACTION_DATE_
protected void onCreate(Bundle
CHANGED);
savedInstanceState) {
package [Link];
[Link](Intent.ACTION_TIME_C
HANGED); import [Link];
import [Link];
[Link](Intent.ACTION_TIMEZ import [Link];
ONE_CHANGED); import [Link];
import [Link];
[Link](Intent.ACTION_SHUTD
OWN); public class SystemBroadcastReciever
// Registering the receiver extends BroadcastReceiver {
registerReceiver(receiver, filter); @Override
Log.d("MainActivity", "Broadcast public void onReceive(Context context,
Receiver Registered"); Intent intent) {
} if (intent != null &&
@Override [Link]() != null) {
protected void onDestroy() { String action = [Link]();
[Link](); Log.d("Testing", "Received
unregisterReceiver(receiver); Broadcast: " + action);
Log.d("MainActivity", "Broadcast [Link](context,
Receiver Unregistered"); "Broadcast Received: " + action,
} Toast.LENGTH_LONG).show();
} }
}
[Link] }

[Link]
<uses-permission android:name="[Link].RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="[Link].ACCESS_NETWORK_STATE"/>
<uses-permission android:name="[Link].BATTERY_STATS"
tools:ignore="ProtectedPermissions" />

Output:

You might also like