Monday, October 15, 2012

How to use Intents between two applications to pass message,data or notification:


what is Intent ? Intent is one of the mechanism to  communicate  between two application or more application.


     Assume Network status notifications are raised as Intent and whoever application handles network, he has to receive the notification and act accordingly. For example, the application which is using internet connection should receive the network connected or disconnected message.


  Sender has to send the data. Receiver application has to receive the intents and receive add filters which need to handle.
   For example, Many intents can be raised from system level. But we need to be notified for particular intents, then those intents are added in IntentFilter, registered along with broadcast receiver.
     In the below code, we are raising WiFI network state changed event from framework or application. Whatever parameters we have to pass we can use putExtra() function. From receiver we have to get the values by getXXXExtra() fns.
    We can also raise custom intents in the similar way.




Sending an intent from an application to pass data or from framework:


CONNECTED
===============
string WIFIManager.NETWORK_STATE_CHANGED_ACTION ="WiFi"

Intent sIntent = new Intent(WIFIManager.NETWORK_STATE_CHANGED_ACTION);
sIntent.putExtra("WiFiUp",true);
SendBroadcast(sIntent);
context.sendBroadcast(sIntent);

DISCONNECTED:
===========
Intent sIntent = new Intent(WIFIManager.NETWORK_STATE_CHANGED_ACTION);
sIntent.putExtra("WiFiUp",false);
context.sendBroadcast(sIntent);


Receiving  Application:
==================

    Message Receiving application should have BroadcastReceiver with Receive() function.  We have to create IntentFilter and add an action to the intentFilter and while registering the broadcast receiver, we have to pass IntentFilter too.


whenever we dont want to receive the intent, we have to unregister the receiver.



import android.content.BroadcastReceiver;

private final BroadcastReceiver mReceiver;
private final IntentFilter mWiFiStateFilter;

 mWiFiStateFilter = new IntentFilter(WIFIManager.WIFI_STATE_CHANGED_ACTION);
 mWiFiStateFilter.addAction(WIFIManager.NETWORK_ENABLED_ACTION);
//we can add many actions in an intent Filter

mReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                handleEvent(context, intent);
            }
        };

private void handleEvent(Context context, Intent intent)
{
        String action = intent.getAction();
        if (action.equals(WIFIManager.NETWORK_STATE_CHANGED_ACTION) )
    {
        mEnabled= intent.getBooleanExtra("WiFiUp", false);
         //Receiving the WiFi status
        }
       else
        if (action.equals(WIFIManager.NETWORK_ENABLED_ACTION) )
       {
       }

   
}

 public void resume() {
         //Registering the receiver.
        //Usually receivers are registered in OnResume()
         mContext.registerReceiver(mWiFiStateReceiver,mWiFiStateFilter);
    }

 public void pause() {
       //Receivers are unregistered in OnPause() fn
         mContext.unregisterReceiver(mWiFiStateReceiver);
    }





No comments: