Android-Broadcast Receiver and Intent Filter

前端 未结 3 530
Happy的楠姐
Happy的楠姐 2020-12-30 12:20

I am new to android platform.please help me out how the Broadcast Receiver and Intent Filter behaves in android.please explain in simple line or with example.thanks in adv

3条回答
  •  轮回少年
    2020-12-30 12:59

    A broadcast receiver is a class in your Android project which is responsible to receive all intents, which are sent by other activities by using android.content.ContextWreapper.sendBroadcast(Intent intent)

    In the manifest file of you receicving activity, you have to declare which is your broadcast receiver class, for example:

    
      
        
      
    
    

    As you can see, you also define the intent filter here, that is, which intents should be received by the broadcas receiver.

    Then you have to define a class which extends BroadcastReceiver. This is the class you defined in the manifest file:

    public class MessageListener extends BroadcastReceiver {
    
    
        /* (non-Javadoc)
         * @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)
         */
        @Override
        public void onReceive(Context context, Intent intent) {
    ...
    }
    

    Here, all intents which are passed through the filter are received and you can access them using the parameter passed in the method call.

提交回复
热议问题