Thursday, December 8, 2011

How to pass and receive custom Intent with BroadcastReceiver


This is outgoing class.which is broadcast the Intent through sendBroadcast method :

public class SendingBroadcastIntent {

 

    public static final String CUSTOM_INTENT = "com.custom.intent.action.TEST";

 

        System.out.println("HIT OUTGOING");

        Intent i = new Intent();

        i.setAction(CUSTOM_INTENT);
        Bundle extras = new Bundle();  
                 extras.putInt("progress", progressPercentage);
                 i.putExtras(extras);

        context.sendBroadcast(i);



}
This is incoming class.which is receive broadcast Intenet :

public class IncomingIntentReceiver extends BroadcastReceiver {


    @Override

    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals(OutgoingReceiver.CUSTOM_INTENT)) {

            System.out.println("RECEIVE THE INTENT");

        }

    }

}
Register Receiver in The Android Manifest:

<receiver android:name=".IncomingIntentReceiver" android:enabled="true">

4 comments:

  1. Thanks Pradeep for sharing your knowledge with us.

    trucking factoring

    ReplyDelete
  2. What's OutgoingReceiver????

    ReplyDelete
    Replies
    1. SendingBroadcastIntent class send broadcast intent with data .you can call this is OutgoingReceiver class .

      you can write down in IncomingIntentReceiver class

      if (intent.getAction().equals(SendingBroadcastIntent.CUSTOM_INTENT)) {

      System.out.println("RECEIVE THE INTENT");

      }

      }

      Delete
  3. This comment has been removed by the author.

    ReplyDelete