Advertisement
Guest User

Untitled

a guest
Nov 11th, 2015
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.18 KB | None | 0 0
  1. package pl.danek.battery;
  2.  
  3. import android.app.IntentService;
  4. import android.app.Notification;
  5. import android.app.NotificationManager;
  6. import android.app.Service;
  7. import android.content.BroadcastReceiver;
  8. import android.content.Context;
  9. import android.content.Intent;
  10. import android.content.IntentFilter;
  11. import android.media.audiofx.NoiseSuppressor;
  12. import android.os.BatteryManager;
  13. import android.os.Handler;
  14. import android.os.IBinder;
  15. import android.support.v4.app.NotificationCompat;
  16. import android.widget.Toast;
  17.  
  18. public class BatteryService extends IntentService{
  19.  
  20.     private BroadcastReceiver receiver = new BroadcastReceiver(){
  21.  
  22.         @Override
  23.         public void onReceive(Context context, Intent intent) {
  24.             calculateBatteryLevel(intent);
  25.         }
  26.        
  27.     };
  28.    
  29.     public static int NOTIFICATION_ID = 12354;
  30.  
  31.     NotificationManager manager;
  32.     Notification notification;
  33.     int batteryLevel;
  34.    
  35.     private boolean finish = true;
  36.    
  37.    
  38.     public BatteryService() {
  39.         super("Battery Service");
  40.     }
  41.  
  42.     @Override
  43.     protected void onHandleIntent(Intent intent) {
  44.         registerReceiver(receiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
  45.  
  46.        
  47.         manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
  48.  
  49.         do {
  50.             showNotification();
  51.             try {
  52.                 Thread.sleep(5000);
  53.             } catch (InterruptedException e) {
  54.                 e.printStackTrace();
  55.             }
  56.         } while (finish );
  57.     }
  58.    
  59.  
  60.     private void calculateBatteryLevel(Intent intent) {
  61.         batteryLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,-1);
  62.     }
  63.  
  64.     private void showNotification() {
  65.         int icon = choseIcon();
  66.         notification = new Notification.Builder(this)
  67.         .setContentTitle("Battery level")
  68.         .setSmallIcon(icon)
  69.         .setOngoing(true)
  70.         .setContentText(batteryLevel + " %")
  71.         .build();
  72.         manager.notify(NOTIFICATION_ID, notification);
  73.     }
  74.  
  75.  
  76.     private int choseIcon() {
  77.         if (batteryLevel < 25) {
  78.             return R.drawable.battery_less;
  79.         }
  80.         if (batteryLevel < 50) {
  81.             return R.drawable.battery_half;
  82.         }
  83.         if (batteryLevel < 75) {
  84.             return R.drawable.battery_more;
  85.         }
  86.         return R.drawable.battery_full;
  87.     }
  88.  
  89.     @Override
  90.     public void onDestroy() {
  91.         super.onDestroy();
  92.         manager.cancelAll();
  93.         finish = false;
  94.     }
  95.    
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement