0% found this document useful (0 votes)
5 views2 pages

Battery Receiver

The document contains Java code for an Android application that displays the battery percentage on the screen. It includes a MainActivity class that registers a BatteryReceiver to listen for battery level changes and updates a TextView accordingly. Additionally, it provides the XML layout for the activity, which consists of a single TextView centered on the screen.

Uploaded by

sanchitahiwrale
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)
5 views2 pages

Battery Receiver

The document contains Java code for an Android application that displays the battery percentage on the screen. It includes a MainActivity class that registers a BatteryReceiver to listen for battery level changes and updates a TextView accordingly. Additionally, it provides the XML layout for the activity, which consists of a single TextView centered on the screen.

Uploaded by

sanchitahiwrale
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/ 2

Java

package com.example.bt;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


TextView tv;
BatteryReceiver batteryReceiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
batteryReceiver = new BatteryReceiver(tv);
registerReceiver(batteryReceiver,new
IntentFilter(Intent.ACTION_BATTERY_CHANGED));

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

add new java Class file names As BatteryReceiver

package com.example.bt;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.TextView;

public class BatteryReceiver extends BroadcastReceiver {


TextView tv;
BatteryReceiver(TextView tv){
this.tv =tv;
}

@Override
public void onReceive(Context context, Intent intent) {
int percentage = intent.getIntExtra("level",0);

if (percentage !=0){
tv.setText(percentage +"%");
}

}
}

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!"
android:id="@+id/tv"
android:textSize="40dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

You might also like