According to google “an Intent object, is a passive data structure holding an abstract description of an operation to be performed”.
intent is a very powerful concept. I have seen following advantages of intents.
1. It loosely couples your application.
2. It can activate three core components of the android applications activities, services, and broadcast receivers.
3. Using intents you can start any other activity that is installed in the system.
e.g. you can start address book application and you can get contacts from address.
4. Using intents you can expose your applications activity for other applications.
e.g. You can register your own “Send SMS by myApp” application for sending SMS.
Intents are of two types.
Explicit Intents: explicit intents used when you know the name of activity that you want to launch.
e.g.
Intent intent = new Intent(FirstActivity.this,SecondActivity.class); startActivity(intent);
Implicit Intents: Instead of giving name of the intent you tell the system what you want to perform and system will find suitable activity for your task.
e.g.
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.vimviv.com")); startActivity(intent);
Some example of explicit intents:
1. Start Activity:
Intent intent = new Intent(FirstActivity.this, SecondActivity.class); startActivity(intent);
2. Get result from activity:
a. FirstActivity.java
public class FirstActivity extends Activity { Button button; private static final int REQUEST_CODE = 10; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout ll = new LinearLayout(this); button = new Button(this); ll.addView(button); setContentView(ll); button.setText("Call Second Activity"); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(FirstActivity.this, SecondActivity.class); i.putExtra("data", "Hi from First Activity"); // Set the request code to any code you like, you can identify the // callback via this code startActivityForResult(i, REQUEST_CODE); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) { if (data.hasExtra("result")) { String message = data.getExtras().getString("result"); Toast.makeText(FirstActivity.this, message, Toast.LENGTH_SHORT).show(); } } } }
Explanation of above code is as below:
setContentView: we have added button to linear layout and after that linear layout to activity.
putExtra: using startActivity we are launching SecondActivity and with putExtra method we are sending data also.
onActivityResult: When Second Activity will finish it will call the onActivityResult method of FirstActivity.
b. SecondActivity.java
public class SecondActivity extends Activity{ Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout ll = new LinearLayout(this); button = new Button(this); ll.addView(button); setContentView(ll); button.setText("back"); String message = getIntent().getExtras().getString("data"); Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { finish(); } }); } @Override public void finish() { Intent data = new Intent(); data.putExtra("result", "Hi from second activity"); setResult(RESULT_OK, data); super.finish(); } }
Explanation of above code is as below:
getExtras: Using the getExtras method we are fetching of data sent by FirstActivity.
finish() :finish method will remove SecondActivity from the display stack and send data to the FirstActivity.
3. Start Service:
Intent intent=new Intent("com.vimviv.service.serviceClass"); startService(intent);
implicit intents examples:
1. make a call:
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(“1234567890”)); startActivity(callIntent);
2. Launch google map
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:37.423156,-122.084917?z=19")); startActivity(intent);
3. email client:
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); emailIntent .setType("plain/text"); emailIntent .putExtra(android.content.Intent.EXTRA_EMAIL, newString[]{"yourmail@website.com"}); emailIntent .putExtra(android.content.Intent.EXTRA_SUBJECT, mySubject); emailIntent .putExtra(android.content.Intent.EXTRA_TEXT, myBodyText); startActivity(Intent.createChooser(intent, "Send mail));
4. start SMS app:
Intent sendIntent = new Intent(Intent.ACTION_VIEW); sendIntent.setData(Uri.parse("sms:")); sendIntent.putExtra("sms_body", x);
5. To start a search in the Internet:
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH ); intent.putExtra(SearchManager.QUERY, "Android"); startActivity(intent);
For implicit intents read my next post What is intent filter in android?
Incoming search terms:
- intent in android (359)
- what is intent in android (293)
- intent android (159)
- intent java (86)
- java intent (76)
- Android intent (61)
- intent in java (42)
- intents in android (41)
- what is an intent in android (21)
- what is intent android (12)