Tuesday, 18 December 2012

Intent Filter


Intent Filters are similar to putting Hooks (SetWindowHook) in windows.

1. Using this we can register for some actions (action can be default actions like battery charged,or invoking a URL or custom action strings).
2. Android will automatically invoke the activity associated that intent filter.

Intent filter is defined in the AndroidManifest.xml file.

Say we want to launch an activity when someone invokes a URL



<activity android:name="Downloader">
   <intent-filter>
      <action android:name = "android.intent.action.VIEW"/>
      <category android:name ="android.intent.category.DEFAULT"/>
      <data android:scheme = "http" />
     <data android:host= "www.google.com" />
    </intent-filter>
 </activity>

In this case our activity name is Downloader. Say it is for downloading the web page associated with URI.
Now if we invoke a URI like this android will automatically show a popup menu which contains the default Browser and our  Downloader activity.


Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.google.com"));
startActivity(intent);

But this will work only for "wwww.google.com" because we provided host name as "www.google.com". Remove that host name for launching our Downloader activity for any URL.

Now let us say we want to launch an activity, and it should be invoked for dialing number. That is for the scheme "tel:" , remember earlier our scheme (data android:scheme ) was "http". Android uses will use this scheme to identify the activities.  In this case intent filters for the CustomDialer activity can be registered like this.


<activity android:name=".CustomDialer">
   <intent-filter>
      <action android:name = "android.intent.action.CALL"/>
      <category android:name ="android.intent.category.DEFAULT"/>
      <data android:scheme = "tel" />
  </intent-filter>
</activity>


Now if someone calls dials a number like this

Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:919494125066"));
android will show a popup menu containing default Dialer  and our CustomDialer activities.




Friday, 23 November 2012

Intent Quick Guide


Intents

Intent are difficult to understand first. I wanted to mention some straightforward uses for Intent. Here it is.

1. Used for communicating between activities.
2. Intent allows to start an activity explicitly or implicit( for this we need to register for particular action)
3. Broadcast message sending is possible with intent.
    for example a new SMS comes. applications can register for such events and act accordingly.

Example 1.  Explicitly opening an activity.

I this like calling dialog.DoModel() (or modeless) in Windows applications. If you are(was) a MFC programmer , you can remember this that way.

public void onCreate(Bundle savedInstanceState)
{
        super.onCreate(savedInstanceState);         
        setContentView(R.layout.main);
        Button id1 = (Button)findViewById(R.id.Button01);
        id1.setOnClickListener(new OnClickListener()
        {

     @Override public void onClick(View v)
             {
Intent startSecond = new Intent(IntentDemo.this,SecondActivity.class);
startActivityForResult(startSecond, 101);
              }
        }
);

Number 101 identifies the request code. When the  "SecondActivity" terminates, framework will call a function onActivityResult in the parent activity to inform about the termination of the SecondActivity.


protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
     super.onActivityResult(requestCode, resultCode, data);
     if (requestCode == 101)
     {   
                    // SecondActivity is just terminated. Do some useful things here.
     }
}


There you can use the that request code to identify the activity which just get terminated.
This is useful in situations where the main activity launches multiple sub activity, and on terminating those main activity need to know it to do some other useful actions..

Example 2.  Implicitly opening an activity.

This allows to launch some activity based on some action. Action strings  are defined in the Intent class. Lot of actions are available in-order to meet the basic programming requirements.

Say to call a number from your activity , you call Intent like below
This will popup the dialer activity , Of-course you need to have some balance to make further calls.


Intent intent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:4545-1334"));
startActivity(intent);

While creating a new Intent , we can pass Uri as data. Uri is very generic. So for each intent you need to find the correct Uri data in-order to use it correctly.