What is Intent?

Monday, February 27, 2012

  • Definition: Intent (android.content.Intent) is an asynchronous message mechanism used by the Android operating system to match task requests with the appropriate Activity or Service (launching it, if necessary) and to dispatch broadcast Intents events to the system at large.
  • Package: android.content.Intent.
 How it is useful for transitioning between various activities?

  • Android app have multiple entry points, No main function.
  • One activity is designated as main / launcher activity in manifest file
  • To transition from one activity to other after creating instance call startActivity() method which has “Intent” instance as argument.
  •  Activity can be launched in many ways using Intent
  1.  Launching new activity by class name (Creating simple intent)
  •  Create instance of Intent with argument as target activity as
              startActivity(new Intent(getApplicationContext()MyDrawActivity.class));
     2.  Creating Intent with Action & Data
  • Intent object are composed of two main parts:
              the action to be performed, and the data to be acted upon.
  • Most common action types : ACTION_MAIN, ACTION_EDIT
  • Here, intent is saying “Do this” (action) “to that” (data).
     3. Launching activity belonging to other application using Intent.
  • Appropriate permission required to access activity of other package.
  • Create instance of Intent with two information : action & data
  • E.g. Intent dial = new Intent(Intent.ACTION_DIAL, number);
     4. Passing additional data using Inent
  • Use Extras property
  • It stores info. In Bundle object
  • Create instance of Intent, use putExtra() method to put some data in form “name/value” pair as two arguments to it.
Receiving & Broadcasting Intent
  •  Broadcast Intent
    • boradcastIntent() method is used.
    • Purpose: To inform other applications about something interesting, use special intent action types.
    • Special intent action e.g. ACTION_BATTERY_LOW, ACTION_BATTERY_CHANGED. (sdcard state changed, application install/uninstall).
     
  • Receiving Broadcasted Intent
    •  Receive the broadcast using BroadcastReceiver

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.