로그인

검색

JAVA/Android
2013.05.22 19:24

PackageBroadcastReceiver 구현

MoA
조회 수 9140 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 게시글 수정 내역 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 게시글 수정 내역 댓글로 가기 인쇄

 private class PackageBroadcastReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {

   if (intent != null) {
    String packageName = intent.getData().getSchemeSpecificPart();
    Log.w(TAG, "onReceive() packageName : " + packageName);

    if (Intent.ACTION_PACKAGE_ADDED.equalsIgnoreCase(intent.getAction())) {
     Log.w(TAG, "onReceive() ACTION_PACKAGE_ADDED");
    } else if (Intent.ACTION_PACKAGE_CHANGED.equalsIgnoreCase(intent.getAction())) {
     Log.w(TAG, "onReceive() ACTION_PACKAGE_CHANGED");
    } else if (Intent.ACTION_PACKAGE_DATA_CLEARED.equalsIgnoreCase(intent.getAction())) {
     Log.w(TAG, "onReceive() ACTION_PACKAGE_DATA_CLEARED");
    } else if (Intent.ACTION_PACKAGE_INSTALL.equalsIgnoreCase(intent.getAction())) {
     Log.w(TAG, "onReceive() ACTION_PACKAGE_INSTALL");
    } else if (Intent.ACTION_PACKAGE_REMOVED.equalsIgnoreCase(intent.getAction())) {
     Log.w(TAG, "onReceive() ACTION_PACKAGE_REMOVED");
    } else if (Intent.ACTION_PACKAGE_REPLACED.equalsIgnoreCase(intent.getAction())) {
     Log.w(TAG, "onReceive() ACTION_PACKAGE_REPLACED");
    } else if (Intent.ACTION_PACKAGE_RESTARTED.equalsIgnoreCase(intent.getAction())) {
     Log.w(TAG, "onReceive() ACTION_PACKAGE_RESTARTED");
    }
   }
  }
 }

 

registerReceiver

 

  mPackageBroadcastReceiver = new PackageBroadcastReceiver();
  IntentFilter intentFilter = new IntentFilter();
  intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
  intentFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
  intentFilter.addAction(Intent.ACTION_PACKAGE_DATA_CLEARED);
  intentFilter.addAction(Intent.ACTION_PACKAGE_INSTALL);
  intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
  intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
  intentFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
  intentFilter.addDataScheme("package");
  registerReceiver(mPackageBroadcastReceiver, intentFilter);

 

unregisterReceiver

 

  if (mPackageBroadcastReceiver != null) {
   try {
    unregisterReceiver(mPackageBroadcastReceiver);
    mPackageBroadcastReceiver = null;
   } catch (Exception e) {
    e.printStackTrace();
   }
  }

manifest 등록

 

        <receiver android:name=".PackageBroadcastReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.ACTION_PACKAGE_ADDED" />
                <action android:name="android.intent.action.ACTION_PACKAGE_CHANGED" />
                <action android:name="android.intent.action.ACTION_PACKAGE_DATA_CLEARED" />
                <action android:name="android.intent.action.ACTION_PACKAGE_INSTALL" />
                <action android:name="android.intent.action.ACTION_PACKAGE_REMOVED" />
                <action android:name="android.intent.action.ACTION_PACKAGE_REPLACED" />
                <action android:name="android.intent.action.ACTION_PACKAGE_RESTARTED" />
                <data android:scheme="package"/>
            </intent-filter>
        </receiver> 

주의 : data scheme를 반드시 설정해야 한다.

?

List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
공지 Tool/etc Programming 게시판 관련 2 MoA 2014.11.01 46572
271 Deeplearning Using Machine Learning to Predict Customers’ Next Purchase Day OBG 2024.02.27 9677
270 서버 멀티-플레이어 게임 서버와 레이턴시 보상 테크닉 OBG 2024.01.16 4884
269 Deeplearning [ifkakao] 추천 시스템: 맥락과 취향 사이 줄타 OBG 2024.01.10 5742
268 Tool/etc How to stop programmers to copy the code from GitHub when they leave the company? OBG 2024.01.02 8062
267 Site 10 Useful/Fun/Weird Github Repos You Have to Play Around With OBG 2023.12.28 7620
266 Site 모든 개발자를위한 10 가지 특별한 GitHub 리포지토리 OBG 2023.12.28 5681
265 Deeplearning 마이크로소프트가 공개한 무료 AI 코스들 OBG 2023.11.28 6239
264 Deeplearning 내 마음대로 선정한 머신러닝/딥러닝 학습 추천 서적 OBG 2023.08.14 6638
263 Deeplearning LSTM-AE를 이용한 시퀀스 데이터 이상 탐지 OBG 2023.08.14 6122
262 Deeplearning Top 3 most used Pytorch Ecosystem Libraries you should Know about OBG 2023.08.02 8308
261 LLM [번역]거대언어모델(LLM) 가이드 OBG 2023.07.20 5297
260 Deeplearning PyTorch 딥러닝 챗봇 OBG 2023.07.04 7526
259 서버 Debugging Node.js Memory Leaks: How to Detect, Solve or Avoid Them in Applications OBG 2023.07.04 5976
258 LLM LLM 출력 속도 24배 높여주는 라이브러리 등장했다 OBG 2023.06.30 6023
257 Deeplearning [한빛미디어] 머신러닝·딥러닝 도서 선택 가이드 OBG 2023.06.11 6432
256 Python Numpy의 axis 변경 OBG 2023.06.09 8075
255 서버 Design a Basic Search Engine (Google or Bing) | System Design Interview Prep OBG 2023.05.27 6500
254 서버 SSH-Tunneling을 통한 MySQL 서버 연결 OBG 2023.04.21 6669
253 Deeplearning Reinforcement Learning for Dynamic Pricing Suggestion OBG 2023.04.01 7527
252 Deeplearning 추천 시스템 OBG 2023.03.30 6894
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 17 Next
/ 17