6 tháng 6, 2014

Uses of Intents

In android we all know how important an Intent class is. I will list down few magical things which you can do using intents.
  1. Display a web page
Uri uri = Uri.parse(“http://www.google.com”);
Intent it  = new Intent(Intent.ACTION_VIEW,uri);
startActivity(it);
  1. Display Specific Location on Google Map
Uri uri = Uri.parse(“geo:38.899533,-77.036476″);
Intent it = new Intent(Intent.Action_VIEW,uri);
startActivity(it);


  1. Display direction on Google Map
“);
Intent it = new Intent(Intent.ACTION_VIEW,URI);
startActivity(it);
  1. Initiate a call from your application
Uri uri = Uri.parse(“tel:09016219994″);
Intent it = new Intent(Intent.ACTION_DIAL, uri);
startActivity(it);
  1. Send an SMS
Uri uri = Uri.parse(“smsto:0800000123″);
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra(“sms_body”, “The SMS text”);
startActivity(it);
  1. Send an MMS
Uri uri = Uri.parse(“content://media/external/images/media/23″);
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(“sms_body”, “some text”);
it.putExtra(Intent.EXTRA_STREAM, uri);
it.setType(“image/png”);
startActivity(it);
  1. Send an E-mail
Uri uri = Uri.parse(“mailto:hardik.trivedi@indianic.com”);
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(it);
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_EMAIL, “me@abc.com”);
it.putExtra(Intent.EXTRA_TEXT, “The email body text”);
it.setType(“text/plain”);
startActivity(Intent.createChooser(it, “Choose Email Client”));
//To put attachment in email.
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_SUBJECT, “The email subject text”);
it.putExtra(Intent.EXTRA_STREAM, “file:///sdcard/mysong.mp3″);
sendIntent.setType(“audio/mp3″);
startActivity(Intent.createChooser(it, “Choose Email Client”));
  1. Play Audio
Intent it = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse(“file:///sdcard/song.mp3″);
it.setDataAndType(uri, “audio/mp3″);
startActivity(it);
  1. Search for an app on android market
Uri uri = Uri.parse(“market://search?q=pname:com.eyet.meinschiff”);
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
  1. Display detail page of app
Uri uri = Uri.parse(“market://details?id=app_id”);
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
//where app_id is the application ID, find the ID
//by clicking on your application on Market home
//page, and notice the ID from the address bar
  1. Search content on Google
Intent intent = new Intent();
intent.setAction(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY,”What is android?”);
startActivity(intent);
  1. Getting an Image from Gallery
Intent intent = new Intent();
intent.setType(“image/*”);
intent.setAction(Intent.ACTION_GET_CONTENT);                   startActivityForResult(Intent.createChooser(intent,
                           “Select Picture”), SELECT_PICTURE);
// Where SELECT_PICTURE is an integer request code.
  1. Open GPS Settings screen
startActivity(newIntent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
  1. Open Camera from app
//define the file-name to save photo taken by Camera activity
String fileName = “new-photo-name.jpg”;
//create parameters for Intent with filename
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION,”Image capture by camera”);
//imageUri is the current activity attribute, define and save it for later usage (also in onSaveInstanceState)
imageUri = getContentResolver().insert(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
//create new Intent
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
// Below is the code for onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
  if (resultCode == RESULT_OK) {
      //use imageUri here to access the image
  } else if (resultCode == RESULT_CANCELED) {
      Toast.makeText(this, “Picture was not taken”, Toast.LENGTH_SHORT);
  } else {
      Toast.makeText(this, “Picture was not taken”, Toast.LENGTH_SHORT);
  }
}
}
  1. Add an Account
startActivity(new Intent(Settings.ACTION_ADD_ACCOUNT));
  1. Open Network Setting or Air Plane mode setting
startActivity(new Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS));
  1. Show settings to allow configuration of Bluetooth
startActivity(new Intent(Settings.ACTION_BLUETOOTH_SETTINGS));
  1. Show settings to allow configuration of date and time
startActivity(new Intent(Settings.ACTION_DATE_SETTINGS));
  1. Show settings to allow configuration of display
startActivity(new Intent(Settings.ACTION_DISPLAY_SETTINGS));
  1. Show settings to allow configuration of locale
startActivity(new Intent(Settings.ACTION_LOCALE_SETTINGS));
  1. Show settings for selecting the network operator
startActivity(new Intent(Settings.ACTION_NETWORK_OPERATOR_SETTINGS));
  1. Show settings to allow configuration of sync settings
startActivity(new Intent(Settings.ACTION_SYNC_SETTINGS));
  1. Show settings to allow configuration of privacy options
startActivity(new Intent(Settings.ACTION_PRIVACY_SETTINGS));
  1. Launching the contact picker
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
      Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
// Below is the code for onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
   if (resultCode == RESULT_OK)
{
       switch (requestCode)
{
               case CONTACT_PICKER_RESULT:
                   Bundle extras = data.getExtras();
Set<string> keys = extras.keySet();
Iterator<string> iterate = keys.iterator();
while (iterate.hasNext())
{
    String key = iterate.next();
Log.v(DEBUG_TAG, key + “[" + extras.get(key) + "]“);
}
Uri result = data.getData();
Log.v(DEBUG_TAG, “Got a result: “
    + result.toString());
break;
           }
       }
else
{
           // gracefully handle failure
      }
}
  1. Show general device information settings (serial number, software version, phone number, etc.).
startActivity(new Intent(Settings.ACTION_DEVICE_INFO_SETTINGS));
  1. Sharing Text
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType(“text/html”);
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(“<p>This is the text that will be shared.</p>”));
startActivity(Intent.createChooser(sharingIntent,”Share using”));


Không có nhận xét nào:

Đăng nhận xét