Blog purpose for android basic example for android app developer. any query please my contact

Sunday 31 May 2020

how to get battery level and state in android studio

how to get battery level and state in android studio

The way devices are managing the robot software is incrementally going better. That means Android battery time is easy getting better. Yet, Androids are even battery vampires. Using an application like JuiceDefender will nearly double the battery time. JuiceDefender achieves greater Android battery time by closing off services, e.g., the screen syncing of data, Wi-Fi, and Bluetooth. Where JuiceDefender is other than that competitor is these available configurations. Depending on how much design you want, there are three other variants of JuiceDefender to choose from. Think which version suits the needs best before you spend the money using the comparison map. In the Ultimate translation, you will decide which apps you need to sync when the information intermittently becomes on. That means you only get that apps with the information most valuable to you, like e-mail or Facebook, go through when information syncs.

 

When you get a specific battery point, the convenient place for iOS and Android devices automatically dials back screen resolution, animations, screen app refreshing and different battery drainers. It’s an astounding expansion of battery life, and Silverman says that feature doesn’ ’t change how fast the telephone charges.

 

For the following method check battery level in android:

 

BatteryManager bm = (BatteryManager)getSystemService(BATTERY_SERVICE);

      if (android.os.Build.VERSION.SDK_INT > = android.os.Build.VERSION_CODES.LOLLIPOP) {

         int percentage = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);

         text.setText("Battery Percentage is "+percentage+" %");

 

Follow the full  source code How to get battery level and state in Android:


1.MainActivity.java:


package com.akash.battrylevel;
import android.os.BatteryManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
   int view = R.layout.activity_main;
   TextView text;

   @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
   @Override

   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      setContentView(view);
      text = findViewById(R.id.text);
      BatteryManager bm = (BatteryManager)getSystemService(BATTERY_SERVICE);
      if (android.os.Build.VERSION.SDK_INT > = android.os.Build.VERSION_CODES.LOLLIPOP) {
         int percentage = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
         text.setText("Battery Percentage is "+percentage+" %");
      }
   }
}


 2.activity_main.xml:

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

<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"

   android:id = "@+id/parent"

   xmlns:tools = "http://schemas.android.com/tools"

   android:layout_width = "match_parent"

   android:layout_height = "match_parent"

   tools:context = ".MainActivity"

   android:gravity = "center"

   android:orientation = "vertical">

   <TextView

      android:id = "@+id/text"

      android:textSize = "18sp"

      android:textAlignment = "center"

      android:text = "batter percentage"

      android:layout_width = "match_parent"

      android:layout_height = "wrap_content" />

</LinearLayout>



No comments:

Post a Comment