Summary of Common Methods of Intent

Posted by hmiller73 on Mon, 05 Aug 2019 13:06:46 +0200

Strongly Recommend Articles: Welcome to Collection
Android Dry Goods Sharing

Read for five minutes, ten o'clock a day, and study with you for life. Here's Android, a programmer.

This paper summarizes the common methods of Intent and encapsulates them into Utils classes.
The main contents are as follows.

  1. Start by component name
  2. Start by package name, class name
  3. Start by class
  4. Phone
  5. Send message
  6. Open Web Page
  7. Play music
  8. Open the picture
  9. Create an alarm clock
  10. create-timer
  11. Add calendar events
  12. Photograph
  13. Open Camera
  14. Turn on the Video Video
  15. Choose contacts
  16. view contact
  17. Edit Contact
  18. Insert contacts
  19. Write an email
  20. Open Map Specified Points
  21. Retrieving specific types of pictures

For an introduction to Intent, see the article
[Intent usage details](
http://www.jianshu.com/p/81e4...

1. Start Activity by Component Name

  • Usage method
    /**
     * Start Activity by Component Name
     * **/
    public static void StartIntentFromComponent(Context context,
            Class intentClass) {
        Intent intent = new Intent();
        // 1. Start Activity with ComponentName
        ComponentName componentname = new ComponentName(context, intentClass);
        intent.setComponent(componentname);
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
        }
    }

2. Start Activity by Package Name and Class Name

  • Usage method
    /**
     * Start Activity with the package name class name
     * **/
    public static void StartIntentFromPackage(Context context,
            String packageName, String className) {
        Intent intent = new Intent();
        // 1. Start Activity with ComponentName
        ComponentName componentname = new ComponentName(packageName, className);
        intent.setComponent(componentname);
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
        }
    }

3. Start Activity by Class

  • Usage method
    /**
     * Start Activity through Class
     * **/
    public static void StartIntentFromClass(Context context, Class<?> classOpen) {
        Intent intent = new Intent();
        // 2. Using the Setclass method, the class method indirectly uses ComponentName
        intent.setClass(context, classOpen);
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
        }
    }

4. Call

  • Using Intent to make a phone call is as follows
    /**
     * Phone
     * **/
    public static void MakeCall(Context context, int number) {

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

        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
                + number));
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
        }

    }

##### Note:
Calling requires permission to apply

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

5. Texting

  • Usage method

1. Basic SMS Sending

    /**
     * 1.Basic Sending SMS
     * **/
    public static void SendMms(Context context, String mmsString) {

        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, mmsString);
        sendIntent.setType("text/plain");
        // sendIntent.setData(Uri.parse("smsto:"));
        // This ensures only SMS apps respond
        // Modify Intnent selector Tittle
        String title = context.getResources().getString(R.string.hello_world);
        Intent chooser = Intent.createChooser(sendIntent, title);

        // Verify whether Activity Receives
        if (sendIntent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(chooser);
        }
    }

2. Custom Sending SMS

    /**
     * 2.Custom Sending SMS
     * **/
    public static void SendMmsCustom(Context context, String mmsString) {

        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, mmsString);
        sendIntent.setType("text/plain");
        // sendIntent.setData(Uri.parse("smsto:"));
        // This ensures only SMS apps respond
        // Modify Intnent selector Tittle

        String title = context.getResources().getString(R.string.hello_world);

        Intent chooser = Intent.createChooser(sendIntent, title);

        // Verify whether Activity Receives
        if (sendIntent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(chooser);
        }
    }

6. Open Web Page

  • Usage method
    /**
     * Open Web Page
     * **/
    public static void OpenInternetUri(Context context, String uri) {

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
        }

    }

7. Playing Music

  • Usage method
    /**
     * Play music
     * **/
    public static void PlayMusic(Context context, String path) {

        // String
        // path=Environment.getExternalStorageDirectory().getAbsolutePath()+"test.mp3";
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.parse("file:///" + path), "audio/*");
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
        }

    }
  • Play an artist-specific album
    /**
     * Search for specific artist albums
     * **/
    public static void playSearchArtist(Context context, String artist) {

        Intent intent = new Intent(
                MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH);
        intent.putExtra(MediaStore.EXTRA_MEDIA_FOCUS,
                MediaStore.Audio.Artists.ENTRY_CONTENT_TYPE);
        intent.putExtra(MediaStore.EXTRA_MEDIA_ARTIST, artist);
        intent.putExtra(SearchManager.QUERY, artist);
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
        }

    }

8. Open the picture

  • Usage method
    /**
     * Open the picture
     * **/
    public static void OpenImage(Context context, File file) {
        // File file =new File("/mnt/sdcard/1.png");
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(file), "image/*");

        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
        }

    }

9. Create an alarm clock

  • Usage method
    /**
     * Create an alarm clock
     * **/

    public static void SetAlarmIntent(Context context, String message,
            int hour, int minutes) {
        Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM)
                .putExtra(AlarmClock.EXTRA_MESSAGE, message)
                .putExtra(AlarmClock.EXTRA_HOUR, hour)
                .putExtra(AlarmClock.EXTRA_MINUTES, minutes);

        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
        
  • Setting the permission of alarm clock action machine
  <!-- Permission to set alarm clock -->
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
        <activity android:name=".Intent.IntentMethod" >
            <intent-filter>
                <action android:name="android.intent.action.SET_ALARM" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
  • Display all alarm clocks

10. Create timers

  • Usage method
    /**
     * create-timer
     * **/
    public static void StartTimer(Context context, String message, int seconds) {
        Intent intent = new Intent(AlarmClock.ACTION_SET_TIMER)
                .putExtra(AlarmClock.EXTRA_MESSAGE, message)
                .putExtra(AlarmClock.EXTRA_LENGTH, seconds)
                .putExtra(AlarmClock.EXTRA_SKIP_UI, true);
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
        }
    }

Add Action to Set SET_TIMER

        <activity android:name=".Intent.IntentMethod" >
            <intent-filter>
                <action android:name="android.intent.action.SET_ALARM" />
                <action android:name="android.intent.action.SET_TIMER" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

11. Add Calendar Events

  • Usage method
    /**
     * Add calendar events
     * **/

    public static void AddCalendarEvent(Context context, String title,
            String location, Calendar begin, Calendar end) {
        Intent intent = new Intent(Intent.ACTION_INSERT)
                .setData(Events.CONTENT_URI).putExtra(Events.TITLE, title)
                .putExtra(Events.EVENT_LOCATION, location)
                .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, begin)
                .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, end);
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
        }
    }
  • Calendar event filtering

12. Photography

  • Usage method
    /**
     * Photograph
     * **/

    public static void CapturePhoto(Context context, String targetFilename,
            Uri mLocationForPhotos) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT,
                Uri.withAppendedPath(mLocationForPhotos, targetFilename));
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
        }

    }
  • Photo filtering

13. Open Camera

  • Usage method

    /**
     * Open Camera
     * **/

    public static void OpenCamera(Context context) {
        Intent intent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
        }
    }
  • Open Camera Filter

14. Turn on the video

  • Usage method

    /**
     * Turn on the video
     * **/

    public static void OpenCameraVideo(Context context) {
        Intent intent = new Intent(MediaStore.INTENT_ACTION_VIDEO_CAMERA);
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
        }
    }
  • Open Video Filtration Function

15. Choose contacts

  • Usage method
    /***
     * Choose contacts
     * **/
    public static void SelectContact(Context context) {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
        }
    }

16. View Contacts

  • Usage method
    /***
     * view contact
     * **/
    public static void ViewContact(Context context, Uri contactUri) {
        Intent intent = new Intent(Intent.ACTION_VIEW, contactUri);
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
        }
    }

17. Editorial Contacts

  • Usage method
    /***
     * Edit Contact
     * **/
    public static void EditContact(Context context, Uri contactUri, String email) {
        Intent intent = new Intent(Intent.ACTION_EDIT);
        intent.setData(contactUri);
        intent.putExtra(Intents.Insert.EMAIL, email);
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
        }
    }

18. Insert contacts

  • Usage method
    /***
     * Insert contacts
     * **/
    public static void InsertContact(Context context, String name, String email) {
        Intent intent = new Intent(Intent.ACTION_INSERT);
        intent.setType(Contacts.CONTENT_TYPE);
        intent.putExtra(Intents.Insert.NAME, name);
        intent.putExtra(Intents.Insert.EMAIL, email);
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
        }
    }

19. Write an email

  • Usage method

    /***
     * Write an email
     * **/
    public static void composeEmail(Context context, String[] addresses,
            String subject, Uri attachment) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("*/*");
        // intent.setData(Uri.parse("mailto:"));
        // only email apps should handle this
        intent.putExtra(Intent.EXTRA_EMAIL, addresses);
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
        intent.putExtra(Intent.EXTRA_STREAM, attachment);
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
        }
    }
  • Mail filtering

20. Open the designated point on the map

  • Usage method
    /***
     * Open Map Specified Points
     * **/
    public static void callCar(Context context, Uri geoLocation) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(geoLocation);
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
        }
    }

Retrieval of specific types of pictures


    /***
     * Retrieving specific types of pictures to get photos
     * **/
    public static void selectImage(Context context) {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
        }
    }

So far, this article is over. If there are any mistakes, you are welcome to make suggestions and corrections. At the same time look forward to your attention, thank you for reading, thank you!

Topics: Android