Saturday, January 20, 2018

Android Intent

Today, I'm going to discuss some small snippet of Intent. An Intent is basically a message that is passed between components (such as Activities, Services, Broadcast Receivers, and Content Providers). One component that wants to invoke another has to only express its intent to do a job. And any other component that exists and has claimed that it can do such a job through intent-filters, is invoked by the Android platform to accomplish the job. This means, neither components are aware of each other's existence but can still work together to give the desired result for the end-user.


An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a Background Service.


Intent perform late runtime binding between the code in different applications.

Action: The general action to be performed, such as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, etc.

Data: The data to operate on, such as a personal record in the contacts database, expressed as a Uri.


Now let's see some code snippet.

1. Open camera.

 public Intent openCamera(Uri mCameraOutput) {
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCameraOutput);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
            cameraIntent.putExtra("android.intent.extras.LENS_FACING_FRONT", 1);
        } else {
            cameraIntent.putExtra("android.intent.extras.CAMERA_FACING", 1);
        }
        return cameraIntent;
    }

2. Open Gallery.

    public Intent getGallery() {
        Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        String[] mimeTypes = {"image/jpeg", "image/jpg", "image/png"};
        galleryIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
        return galleryIntent;
    }



3. Open dialer screen.

@Override
    public void call(String mobile) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        intent.setData(Uri.parse("tel:" + mobile));
        startActivity(intent);
    }

4. Move to google play store.

@Override
    public void moveToGooglePlayStore() {
        if (fragmentBaseActivity != null) {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + fragmentBaseActivity.getPackageName())));
        }
    }

5. Send a mail.

/**
     * Send invitation email.
     */
    private void onShareClick() {

            Intent email = new Intent(Intent.ACTION_SEND);
            email.putExtra(Intent.EXTRA_SUBJECT, fragmentBaseActivity.getString(R.string.share_referral_code));
            email.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(new StringBuilder()
                    .append("<html>")
                    .append("<p>" + fragmentBaseActivity.getString(R.string.hi) + "</p>")
                    .append("<p>" + fragmentBaseActivity.getString(R.string.refer_content) + "</p>")
                    .append("<p><h2>" + mReferCode + "</h2></p>")
                    .append("<p>" + fragmentBaseActivity.getString(R.string.app_google_play_store_content) + "</p>")
                    .append("<p>" + fragmentBaseActivity.getString(R.string.app_link)+" "+ "<a href=>" + "http://play.google.com/store/apps/details?id=" + fragmentBaseActivity.getPackageName() + "</a></p><br>")
                    .append("<p>" + fragmentBaseActivity.getString(R.string.regards) + "</p>")
                    .append("<p>" + mPresenter.getLoginUserName() + "</p>")
                    .append("</html>")
                    .toString()));

            email.setType("text/html");
            startActivity(Intent.createChooser(email, "Choose an Email client :"));
     
    }

Share:

0 comments:

Post a Comment

We're Social

Categories

Ad here

  • Cover art Blend

    Blend app is very advanced app there are lots of features like shayari, status, chat, feed....