Intent Example

What is an Intent ?

Intent is an object that is used to communicate with different Android Components like Activity , Services ,BroadcastReceiver etc..

There are two type of Intent :-

1. Implicit Intent
2. Explicit Intent

// Implicit intent example

1. Open Url

     Intent in=new Intent(Intent.ACTION_VIEW,
     Uri.parse("https://deepsingh44.blogspot.com/"));
     startActivity(in);

2. Open camera

    Intent openCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(openCamera, CAMERA_PIC_REQUEST);

3. Open Gallery

    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select image to upload "),CHOOSE_FILE);

4. Dial Phone number

    Intent in = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + "Phone_Number"));
    startActivity(in);
 
5. Call 
 
    Intent in = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "Phone_Number"));
    startActivity(in);

    uses-permission android:name="android.permission.CALL_PHONE"

6. Send Mail

    Intent email = new Intent(Intent.ACTION_SENDTO);
    email.setData(Uri.parse("mailto: xyz@gmail.com"));
    startActivity(Intent.createChooser(email, "Send Enquiry.."));

7. Share Content
 
    Intent si = new Intent(android.content.Intent.ACTION_SEND);
    si.setType("text/plain");
    String body = "Something wants to share...";
    si.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subuject ");
    si.putExtra(android.content.Intent.EXTRA_TEXT, body);

8. Send Sms
 
    Intent sms = new Intent(android.content.Intent.ACTION_VIEW);
    sms.setType("vnd.android-dir/mms-sms");
    sms.putExtra("address","receiver phoneNumber here");       
    sms.putExtra("sms_body","Message Here...");
    startActivity(sms);



 // Explicit intent example

Intent in=new Intent(getApplicationContext(),SecondActivity.class);
startActivity(in);


No comments: