What is The Intent ?

What is the intent explain with example ?
  • An intent is exactly describe its an intention to do an action.
  • An intent is basically a message to say you did or want to something to happen. But It depend on intents, your app or OS will listen it or react accordingly.
  • To listen for a intent( like the v, or an notification or sms received), you implement broadcast receiver.
  • An Intent are object of the android.content.intent type. And also intent can content data via a Bundle, and this data will be used by the receiver side component.
  • There are two type of intent.An android support two type intent one is the Implicit and another is the explicit

  • Implicit Intent : 
  • An implicit intent is the specify the action which should be performed an optionally data which provide the action.
  • For example, you may write to code for view web page.

Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.bhadreshtech.in"));
startActivity(intent);
  • if an implicit intent sent is sent to the android system, it search for all components which are registered for the specific action and the fitting data type.
  • If only one component is found then they select directly. If several components are identified by the Android System, the user will get a selection dialog and can decide which component should used for intent.
  • Implicit Intent : 
  • Explicit intents define the component which should be called by the Android System,by using java class as identifier .
  • Following should explain how to create explicit intent and send it to the android system. If the class specified in the intent represents and activity, the android system start it.
ntent intent=new Intent(this,ActivityTwo.class);
startActivity(intent);

  • Data transfer with intent between tow activities.
  • Lots of application allow to share data with other people, like whatsapp, facebook, twitter, e.t.c,..
  • Using bundle you can send data with intent with different different types.
  • For example, Sender Side : 

Intent intent = new Intent(context,ActivityTwo.class);
intent.putExtra("key","value");
startActivity(intent);
  • Receiver Side :
Bundle bundle = getIntent().getExtras();
String value=bundle.getString("key");

No comments: