Simple Intent operations

An Intent is a description of an operation or action to be performed. It can be used to launch an activity, to start or interact with a background service, or to send data to Broadcast receiver components. Below are some code snippets using Intent for commonly performed operations in android.

Move from one Activity to another
Suppose you have two Activity class, MainActivity and NextActivity, and on a button click event in your current activity(MainActivity), you want to go to the second activity(NextActivity) . To do this use setOnClickListener(OnClickListener) for the button and in the event onClick(View) use codes given below.

Intent myintent = new Intent();
myintent.setClass(getApplicationContext(), NextActivity.class);
startActivity(myintent);

You also need to add your new Activity to the manifest as another activity like this:

 <activity android:name=".NextActivity" />

Open a URL in browser
Suppose you have two TextView and when it is clicked you want to open the URL http://www.sketchwarehelp.com when the TextView is clicked. Then in TextView onClick event, use following codes.

Intent myintent = new Intent();
myintent.setAction(Intent.ACTION_VIEW);
myintent.setData(Uri.parse("http://www.sketchwarehelp.com"));
startActivity(myintent);

Send text in EditText as email
To send contents of an EditText edittext1 to several different email ids using the email client in device, use following code when send button is clicked.

// List all emails to which mail is to be sent
String[] emails = {"abc@gmail.com","xyz@yahoo.com","mailid@yahoo.com"};
// Write the subject of email
String subject = "This is Subject";
// Get the contents to be sent as email from EditText
String message = edittext1.getText().toString();
// Use Intent to send email through email client in device
Intent myintent = new Intent(Intent.ACTION_SEND);
myintent.putExtra(Intent.EXTRA_EMAIL, emails);
myintent.putExtra(Intent.EXTRA_SUBJECT, subject);
myintent.putExtra(Intent.EXTRA_TEXT, message);
myintent.setType("message/rfc822");
startActivity(Intent.createChooser(myintent, "Choose Email Client"));

Call a number
Use following code on button click event.

Intent myintent = new Intent();
myintent.setAction(Intent.ACTION_CALL);
myintent.setData(Uri.parse("tel:"+"8802177690"));
startActivity(myintent);

You also need to add CALL_PHONE permissions in the manifest file.

 <uses-permission android:name="android.permission.CALL_PHONE" />

Open number in dialer
Use following code on button click event.

Intent myintent = new Intent();
myintent.setAction(Intent.ACTION_DIAL);
myintent.setData(Uri.parse("tel:"+"8802177690"));
startActivity(myintent);

You also need to add CALL_PHONE permissions in the manifest file.

 <uses-permission android:name="android.permission.CALL_PHONE" />

Share text
Suppose you have title in an EditText edittext1, and text to be shared in another EditText edittext2. Then to share the text in edittext2, use following code on button click event.

// Get text from the EditText fields
String title = edittext1.getText().toString();
String message = edittext2.getText().toString();
// Use Intent to share the text
Intent myintent = new Intent(Intent.ACTION_SEND);
myintent.setType("text/plain");
myintent.putExtra(Intent.EXTRA_SUBJECT, title);
myintent.putExtra(Intent.EXTRA_TEXT, message);
startActivity(Intent.createChooser(myintent,"Share using"));

Leave a Reply