Skip to content
Search

Android.content.Intent

lastmod 2018-08-17created 2016-05-244 edits

Sharing the Video type with an Intent

With Intent.createChooser(), the 'Always' and 'Just once' buttons do not appear when choosing an application.

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("video/mp4");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(resultFilePath));
startActivity(Intent.createChooser(intent, getString(R.string.diallog_share_video)));

If you send it as intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(path));, the file may not be attached to the E-Mail.

To play the video in VideoView, enter the following.

VideoView videoView = (VideoView)findViewById(R.id.videoView1);
Uri uri = Uri.parse(MOVIE_URL);
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));

Launching an external application from code

intent = new Intent();
ComponentName cn = new ComponentName(패키지명, 엑티비티명);
intent.setComponent(cn);  
startactivity(intent);

Or

intent.setPackage("com.kakao.talk");

Or

PackageManager pm = getPackageManager();
String packageName = "패키지명";
ApplicationInfo ai = null;
try {
    ai = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
} catch (NameNotFoundException e) {
    e.printStackTrace();
}
String labelName = pm.getApplicationLabel(ai).toString();
Intent intent = pm.getLaunchIntentForPackage(packageName);
startActivity(intent);

Integrating with the Market

/** 마켓으로 이동한다. */
public static void jumpMarket(Context c, String package_name) {
    Uri uri = Uri.parse(kPlayStoreUrlPrefix + package_name);
    c.startActivity(new Intent(Intent.ACTION_VIEW, uri));
}

Launching an external app

/** 어플을 실행한다. */
public static void runApplication(Context c, String package_name) throws PackageManager.NameNotFoundException {
    PackageManager pm = c.getPackageManager();
    Intent intent = pm.getLaunchIntentForPackage(package_name);
    if (intent == null) {
        // Exception이 발생되지 않을 경우 직접 발생시킨다.
        throw new PackageManager.NameNotFoundException("Package(" + package_name + ") not found.");
    } else {
        c.startActivity(intent);
    }
}

Sending a MESSAGE via INTENT

public static void sendMessageFromChooser(Context c, String title, String subject, String msg) {
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    intent.putExtra(Intent.EXTRA_TEXT, msg);
    c.startActivity(Intent.createChooser(intent, title));
}

Attaching an MP4 via INTENT

public static void sendMp4(Context c, String mp4path) {
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("video/mp4");
    intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(mp4path)));
    c.startActivity(intent);
}

Attaching an MP4 via INTENT, using a Chooser

With a Chooser, the OK/Cancel buttons do not appear when the popup shows up through the Intent.

public static void sendMp4FromChooser(Context c, String mp4path, String title) {
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("video/mp4");
    intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(mp4path)));
    c.startActivity(Intent.createChooser(intent, title));
}

Attaching an MP4 via INTENT directly to the App of a specific package

public static void sendMp4FromPackage(Context c, String mp4path, String pkgname) {
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("video/mp4");
    intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(mp4path)));
    intent.setPackage(pkgname);
    c.startActivity(intent);
}

Open the built-in gallery, pick a photo, then set the data using the URI of the photo that was picked.

Intent i = new Intent(Intent.ACTION_PICK);
i.setType(android.provider.MediaStore.Images.Media.CONTENT_TYPE);
i.setData(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); // images on the SD card.
startActivityForResult(i, REQ_CODE_PICK_PICTURE); // 결과를 리턴하는 Activity 호출.

How to get the photo data from the gallery list view.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQ_CODE_PICK_PICTURE) {
        if (resultCode == Activity.RESULT_OK) {
            ImageView img = (ImageView) findViewById(R.id.image);
            img.setImageURI(data.getData()); // 사진 선택한 사진URI로 연결하기
        }
    }
}

See also

Favorite site

References


  1. Handling_screen_off_and_screen_on_intents.pdf ↩

  2. Intent_flag_list_arrangement.pdf ↩