Monday, September 18, 2017

android - Intent to share on facebook messenger

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#b8d1b0"
    >
    <Button
        android:id="@+id/btn_share"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Share on Facebook Messenger"
        android:layout_gravity="center"
        />
</LinearLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private Button mButtonShare;

    // To share app on facebook messenger
    public static final String GOOGLE_PLAY_STORE_URI = "http://play.google.com/store/apps/details?id=";
    public static  final String FACEBOOK_MESSENGER_PACKAGE = "com.facebook.orca";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from xml layout
        mButtonShare = findViewById(R.id.btn_share);

        // Set a click listener for share button
        mButtonShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                shareOnFacebookMessenger();
            }
        });
    }

    // Custom method to share link on facebook messenger
    protected void shareOnFacebookMessenger(){
        /*
            Intent
                An Intent is a messaging object you can use to request an action from another app
                component. Although intents facilitate communication between components in
                several ways, there are three fundamental use cases: Starting an activity, Starting
                a service and Delivering a broadcast.
        */
        Intent sendIntent = new Intent();
        /*
            String ACTION_SEND
                Activity Action: Deliver some data to someone else. Who the data is being delivered
                to is not specified; it is up to the receiver of this action to ask the
                user where the data should be sent.
        */
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT,
                "Check this app on: "
                        +GOOGLE_PLAY_STORE_URI
                        +getPackageName()
        );
        sendIntent.setType("text/plain");

        /*
            Intent setPackage (String packageName)
                (Usually optional) Set an explicit application package name that limits the
                components this Intent will resolve to. If left to the default value of null,
                all components in all applications will considered. If non-null, the Intent
                can only match the components in the given application package.

            Parameters
                packageName String: The name of the application package to handle the intent,
                or null to allow any application package.
            Returns
                Intent Returns the same Intent object, for chaining multiple calls
                into a single statement.
                This value will never be null.
        */
        sendIntent.setPackage(FACEBOOK_MESSENGER_PACKAGE);
        try {
            startActivity(sendIntent);
        }
        catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(mContext,"Please install facebook messenger.",Toast.LENGTH_LONG).show();
        }
    }
}