How to access the battery level and ring an alarm automatically in your Android App? – complete code

This video shows the steps to access the battery level of your Android Phone in your App. Then it shows based on certain level condition how one can do certain operation such as updating the text or ringing an alarm in your Android Phone.

As discussed in this video,
if you want to know how to send SMS automatically from Android App then please watch below:
https://youtu.be/pajvuBZc2WA

if you want to know how to access SMS or read SMS in your Android App then watch below:
https://youtu.be/K9IloJ_66WI

For any query, suggestions or appreciations, we will be glad to hear from you at: programmerworld1990@gmail.com

Source Code:

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.os.BatteryManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private TextView textView;
private Ringtone ringtone;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textView = findViewById(R.id.textView);
ringtone = RingtoneManager.getRingtone(getApplicationContext(), RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE));
BroadcastReceiver broadcastReceiverBattrery = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Integer integerBatteryLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
textView.setText(integerBatteryLevel.toString());
if (integerBatteryLevel [GREATER THAN] 99){
ringtone.play();
}
}
};
registerReceiver(broadcastReceiverBattrery, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}

public void stopButton(View view){
ringtone.stop();
}
}

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
package=”com.example.myapplication”>

<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”>
<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>

<?xml version=”1.0″ encoding=”utf-8″?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
xmlns:app=”http://schemas.android.com/apk/res-auto&#8221;
xmlns:tools=”http://schemas.android.com/tools&#8221;
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”>

<TextView
android:id=”@+id/textView”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Hello World!”
android:textSize=”36sp”
app:layout_constraintBottom_toBottomOf=”parent”
app:layout_constraintLeft_toLeftOf=”parent”
app:layout_constraintRight_toRightOf=”parent”
app:layout_constraintTop_toTopOf=”parent” />

<Button
android:id=”@+id/button”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”165dp”
android:layout_marginTop=”120dp”
android:layout_marginEnd=”158dp”
android:layout_marginBottom=”173dp”
android:onClick=”stopButton”
android:text=”@string/stop”
app:layout_constraintBottom_toTopOf=”@+id/textView”
app:layout_constraintEnd_toEndOf=”parent”
app:layout_constraintStart_toStartOf=”parent”
app:layout_constraintTop_toTopOf=”parent” />

</androidx.constraintlayout.widget.ConstraintLayout>

Leave a Reply

%d bloggers like this: