요즘 핸드폰에 다 있는 액정밝기 자동조절 기능을 하는 앱을 만들어보고 있습니다.
폰에 달린 조도센서를 이용해서 액정밝기를 조절하려고 하는데요
메인 Activity 에서는 자동으로 조절이 되는데 백그라운드로 넘기질 못하겠네요..
Test2Activity.java
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(Test2Activity.this, Test2Service.class);//상태바 내려서 클릭했을 때 이동할 화면
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
switch(arg0.getId())
{
case R.id.button1:
int SysBackLightValue = (int)(BackLightValue * 255);
System.putInt(cResolver, System.SCREEN_BRIGHTNESS, SysBackLightValue);
break;
case R.id.button2:
Notification noti = new Notification(R.drawable.status_image,
"자동밝기조절", java.lang.System.currentTimeMillis()); //상단 상태바의 그림과 문자열 지정
startService(intent);
mSensorManager.unregisterListener(this);
PendingIntent content = PendingIntent.getActivity(Test2Activity.this, 0, intent, 0);//상태바 내려서 클릭하면 어플화면으로 이동
noti.setLatestEventInfo(getApplicationContext(), "자동밝기조절", " ", content);//상태바를 내렸을 때 문자열들과 클릭했을때 뜨는 화면 설정
mNotiManager.notify(NAPNOTI, noti);
break;
case R.id.button3:
stopService(intent);//인텐트 종료
mNotiManager.cancel(NAPNOTI);
break;
}
}
button2를 눌렀을 때 인텐트를 이용하여 서비스를 불러옵니다.
Test2Service.java
public class Test2Service extends Service implements SensorEventListener { boolean able; SensorManager mSensorManager; Sensor mLight; ContentResolver cResolver; WindowManager.LayoutParams layoutParams; BackLight runnable; Thread thread;
public void onCreate() { super.onCreate(); mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); runnable = new BackLight(this, mHandler); thread = new Thread(runnable); }
//stopService 실행시 실행 public void onDestroy() { super.onDestroy(); able=false; if (mSensorManager != null) mSensorManager.unregisterListener(this); }
//startService실행시 실행 public int onStartCommand (Intent intent, int flags, int startId) { able=true; super.onStartCommand(intent, flags, startId); if (mLight != null) mSensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL); // 레지스터 생성 thread.setDaemon(true); thread.start(); return START_STICKY; }
class BackLight extends Activity implements Runnable { Test2Service mParent; Handler mHandler; String name; float brightness; public BackLight(Test2Service parent, Handler handler) { mParent = parent; mHandler = handler; try{ Toast.makeText(Test2Service.this, mLight.getName(), 0).show(); cResolver = getContentResolver(); layoutParams = getWindow().getAttributes(); } catch(Exception e ) { Toast.makeText(Test2Service.this, "BackLight Constructor Exception", 0).show(); } }
public void run() { while(able){ mHandler.sendEmptyMessage(0); try { Thread.sleep(5000);} catch (Exception e) { ; } } } public void setBrightness(BackLight runnable) { try{ layoutParams.screenBrightness = runnable.brightness; //속성 변수의 멤버인 밝기값을 지정 getWindow().setAttributes(layoutParams); //어플 창에 속성변수 적용 System.putInt(cResolver, System.SCREEN_BRIGHTNESS, (int)(255*runnable.brightness)); } catch(Exception e){ Toast.makeText(Test2Service.this,"setBrightness Method Exception", 0).show(); } } }
Handler mHandler = new Handler() { public void handleMessage(Message msg) { if (msg.what == 0) { runnable.setBrightness(runnable); } } };
지저분한 소스코드 길게 올려서 죄송합니다;;;;
Test2Service.java에 BackLight라는 클래스를 하나 더 만들었는데, 액정 밝기를 설정하려면 getWindow() 함수가 필요해서 Activity 클래스를 상속받았고 스레드로 돌려야 할것 같아서 Runnable 인터페이스를 implements 했습니다.
그런데 try-catch 구문 2 부분에서 자꾸 NullPointerException이 나네요....
try-catch 안썻을땐 버튼 누르면 작동이 중지되었다는 메세지창 뜨면서 뻗어버립니다.
layoutParam 과 cResolver 지정에서 문제가 생기는 듯 한데 어떻게 조치해야될지 모르겠습니다.
혼자서 이것저것 찾아보다가 도저히 안되겠에서 부탁드립니다.... 폰에 기본적으로 있는 기능들조차 구현 못하고 있으니 속상하네요...