Skip to main content

Get Battery Level in your app

I've searched and found a sample to get battery level in the screen . What is Broadcast Receivers? Broadcast Receivers is an Android implementation of system-wide publish/subscribe mechanism. The system itself broadcasts events all the time. For example, when an SMS arrives, or call comes in, or battery runs low, or system gets booted, all those events are broadcasted and any number of receivers could be triggered by them. You can also send your own broadcasts from one part of your application to another, or a totally different application. Broadcast receivers themselves do not have any visual representation nor are they actively running in memory. But when triggered, they get to execute some code, such as start an activity, a service, or something else what we defined. To know battery level , I used the following snippet in onCreate() method,


registerReceiver("put your Broadcast receiver object here", new IntentFilter(Intent.ACTION_BATTERY_CHANGED));


and in , onReceive() method ,I did the following,
public void onReceive(Context c, Intent i) {
   int level = i.getIntExtra("level", 0);
   ProgressBar pb = (ProgressBar) findViewById(R.id.progressbar);
   pb.setProgress(level);
   TextView tv = (TextView) findViewById(R.id.textfield);
   tv.setText("Battery Level: " + Integer.toString(level) + "%");
  }

 };
Full Source Code click here

Comments

Popular posts from this blog

Circular Seek Bar - Example

MyView.java package com.rakesh.androidcircularseekbar; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; public class MyView extends View {     /** The context */     private Context mContext;     /** The listener to listen for changes */     private OnSeekChangeListener mListener;     /** The color of the progress ring */     private Paint circleColor;     /** the color of the inside circle. Acts as background color */     private Paint innerColor;     /** The progress circle ring background */     private Paint circleRing;   ...

SSL pinning in Android - A brief discussion

HTTP protocol Communication between the client and a server typically non-encrypted or plain text while we use HTTP protocol.  Pitfall Any middle hacker can interrupt the connection between the client and server and manipulate the data as it involves no encryption. How to overcome this ? As the domain owner one can purchase a digital certificate from CA(Certificate Authority) who are considered as trusted.  A certificate will contain the Owner's name, public key , Issuer's(CA's) name,Issuer's(CA's) signature, domain details, expiry date etc . After the SSL/Leaf certificate is associated with a domain,the communication between client and server will be encrypted. Now the HTTP will become HTTPs. Note : Associating the SSL certificate means it enable the encryption between client and server but does not mean ,the domain owner will never misuse your personal information. How does SSL work ? Pitfall There is a problem here. Let's assume that there is a hacker comes i...

Manual Dependency Injection- Realtime Explanation

Assume that we have a Login Feature in Android application and it will have the classes based on the Android's recommended architecture such as MVVM. --> The  LoginActivity has the dependency of LoginViewModel . --> The LoginViewModel has the dependency of UserRepository --> The UserRepository has the dependency of UserLocalDataSource and UserRemoteDataSource UserRepository.kt class UserRepository ( private val localDataSource : LocalDataSource, private val remoteDataSource : RemoteDataSource ) { // Manipulating data from local and/or remote data sources. } Data Sources class LocalDataSource {} class RemoteDataSource ( private val retrofitLoginService : RetrofitLoginService) { } LoginViewModel.kt class LoginViewModel ( private val userRepository : UserRepository) { //Invocation of methods in UserRepository class } LoginActivity.kt class LoginActivity : AppCompatActivity { private var lateinit loginViewModel: LoginViewModel; public fun onCreate (bun...