- 안드로이드 어플리케이션에서 배터리 상태를 체크하기 위한 방법 -

 

BatteryManager class 사용하여 구현

(developer site link)

http://developer.android.com/reference/android/os/BatteryManager.html

 

방법은 구현하고자 하는 app. 에 BroadcastReceiver 를 추가하여, 'ACTION_BATTERY_CHANGED' 가 올때, 필요한 값들을 체크하여 사용하면 된다.

(ACTION_BATTERY_CHANGED 관련 developer site link)

http://developer.android.com/reference/android/content/Intent.html#ACTION_BATTERY_CHANGED

 

아래 샘플코드에서는 연결상태, 배터리상태, 배터리레벨 에 대해서 확인이 가능하도록 만들었다.

로그상 값만을 간단히 확인 가능하도록 만들었으며, 확장을 위해 context 등 불필요 코드가 몇가지 있으나, 실행에는 무관하다.

 

// 추가 : BroadcastReceiver 의 용법에 대해 주의해서 봐둔다.

 

UI 는 없으며, 아래 같이 로그에서만 확인이 가능함을 밝힌다.

 

 

--------------------------------------------------------------------------------------------------------------------------

 

 

 

Colorized version of the entered code

package com.BatteryChecker;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class BatteryCheckMain extends Activity
{
 private static final String tag = "BatteryChecker";
 private static final String filetag = "<BatteryCheckMain> / ";
 
 private Context context;
 private BroadcastReceiver mBatteryBroadcastReceiver;
 
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  
  context = this;
  
  initBroadcastReceiver(context);
  registerBatteryReceiver(context);
  
  Log.i(tag, filetag + "PLUGED:" + BatteryManager.EXTRA_PLUGGED + "/LEVEL:" + BatteryManager.EXTRA_LEVEL
    + "/STATUS:" + BatteryManager.EXTRA_STATUS);
  
  setContentView(R.layout.main);
 }
 

 @Override
 protected void onDestroy()
 {
  // TODO Auto-generated method stub
  super.onDestroy();
  Log.i(tag, filetag + "onDeatroy()");
  unregisterReceiver(mBatteryBroadcastReceiver);
 }


 @Override
 protected void onPause()
 {
  // TODO Auto-generated method stub
  super.onPause();
 }


 @Override
 protected void onResume()
 {
  // TODO Auto-generated method stub
  super.onResume();
 }





 
 // LOCAL FUNCTION -----------------------------------------------------------------------------
 
 
 // make battery broadcast receiver
 boolean initBroadcastReceiver(Context context)
 {
  mBatteryBroadcastReceiver = new BroadcastReceiver()
  {

   @Override
   public void onReceive(Context context, Intent intent)
   {
    
    final String action = intent.getAction();
    
    Log.i(tag, filetag + "mBatteryBroadcastReceiver - onReceive()/action:" +action);
    
    if(Intent.ACTION_BATTERY_CHANGED.equals(action))
    {
     Log.i(tag, filetag + "PLUG:" + intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0)
       + " / STATUS:" + intent.getIntExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_UNKNOWN)
       + " / LEVEL:" + intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0));
    }
   }

   @Override
   public IBinder peekService(Context myContext, Intent service)
   {
    // TODO Auto-generated method stub
    return super.peekService(myContext, service);
   }
   
  };
  return true;
 } //initBroadcastReceiver
 
 boolean registerBatteryReceiver(Context context)
 {
  Log.i(tag, filetag + "registerBatteryReceiver()");
  
  IntentFilter filter = new IntentFilter();
  filter.addAction(Intent.ACTION_BATTERY_CHANGED);
  filter.addAction(Intent.ACTION_BATTERY_LOW);
  registerReceiver(mBatteryBroadcastReceiver, filter);
  return true;
 }
}


 

 

+ Recent posts