주기적으로 체크하여 BR이 등록되어 있지 않으면 다시 등록을 시켜 주는걸 만드려 하는데
구글링을 해보니 다음과 같은 방법을 통한 꼼수가 있었다.
try { context.unregisterReceiver(screenOnBR); } catch (IllegalArgumentException e){ } catch (Exception e) { Log.e(LOG_NAME, "registScreenOnBR Exception - " + e.getMessage()); } finally { IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_SCREEN_OFF); context.registerReceiver(screenOnBR, filter); }
하지만 다음과 같은 방법을 찾아 이게 더 편하다는걸 알았다.
screenIntent = new Intent(context, screenOnBR.getClass()); screenSender = PendingIntent.getBroadcast(context, 0, screenIntent, PendingIntent.FLAG_NO_CREATE); if (screenSender == null) { IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_SCREEN_OFF); context.registerReceiver(screenOnBR, filter); }
== 추가 ==
해당 기술을 이용해 어플을 구동시 Log를 남기는 어플을 만들어 보았다.
[Android] 구동한 앱을 카운트하는 프로그램 (AppCounter)
========
댓글
댓글 쓰기