2016-07-20 2 views
1

브로드 캐스트 수신기 & 필터를 구현하여 다중 intents에 응답 할 수있는 방법은 무엇입니까?여러 동작이있는 Android 브로드 캐스트 수신기

private BroadcastReceiver myReceiver; 
IntentFilter myFilter = new IntentFilter(); 

에서 onCreate() : 내 조각에서

myFilter.addAction("first"); 
    myFilter.addAction("second"); 

    myReceiver = new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       // do different actions according to the intent 
      } 
     }; 

    registerReceiver(myReceiver, myFilter); 

:

Intent i = new Intent("first"); sendBroadcast(i); 

Intent i = new Intent("second"); sendBroadcast(i); 

감사

+0

단지) (의도'intent.getAction의 행동을보고 ' – tyczj

답변

4
@Override 
public void onReceive(Context context, Intent intent) { 
    String action = intent.getAction(); 
    if(action != null) { 
     if(action.equals("action1") { 
      // CODE 
     } else if (action.equals("action2") { 
      // CODE 
     } 
    } 
} 
관련 문제