Tuesday, 29 July 2014

Facebook SDK Integration in Android Tutorial Part 1

First of all we need to Sign in into https://developers.facebook.com/  website .

After Sign in we need to select Create a New App option under Apps drop down menu as follows

After selecting Create a New App option we will get Create a New App window .

In this window we can see two input fields called Display Name and Name Space
we need to enter only our application name in Display Name field
and we can leave Name Space field empty.
Next choose Category that our application belongs to and then finally click Create App button to finish. 
 Once select Create App button in next window we can see application dashboard as follows.
and in this dashboard only we can get our application id and App Secret

 Next step is in this dashboard we need select Settings option from left side navigation bar as follows

Next we need to select  +Add Platform option as follows

After selecting Add Platform option we will get Select Platform window there we need to select Android platform as follows.

In the next window please provide the application package name and key hashes and select single sign on option and then finally select save changes button as shown in below

Note: to generate key hash for your application please read this post in my blog
http://ramsandroid4all.blogspot.in/2014/07/generating-facebook-key-hash.html


Next download android facebook sdk from here : https://developers.facebook.com/docs/android/downloads

after download we will get facebook sdk zip file just extract it.
Next import facebook sdk into Eclipse as follows
1.under package explorer right click and select import option.
2.next under android folder select Existing Projects into Workspace option.
3.Next select root directory option and browse facebook sdk folder.
4.once we browse android sdk folder it will show you number projects and can import them all
   and you can run them as sample facebook projects or you can just select facebook sdk project and
   hit finish button


Next we need to add facebook sdk library to our project to that follow below steps:
1.Right click on the our project and select properties option
2.In the next window select android
3.under library select add option next it will show you facebook sdk select it and select apply button  
   and select ok button . as shown in below






Monday, 28 July 2014

Generating Facebook Key Hash

package com.ram.facebookkeyhash;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import com.ram.demo1.R;
import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.Signature;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;

public class MainActivity extends Activity
{
    // Declare your application package name
    String packageName = "your application package name";

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

        try
        {
            // Overall information about the contents of a package. This
            // corresponds to all of the information collected from
            // AndroidManifest.xml.
            PackageInfo packageInfo = getPackageManager().getPackageInfo(packageName,
                    PackageManager.GET_SIGNATURES);

            // Array of all signatures read from the package file. This is only
            // filled in if the flag GET_SIGNATURES was set.
            for (Signature signature : packageInfo.signatures)
            {
                // The basic pattern to digest an InputStream
                MessageDigest md = MessageDigest.getInstance("SHA");
                // The basic pattern to digest an InputStream
                md.update(signature.toByteArray());
                // Prints key hash in logcat
                Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
            }

        } catch (NameNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Tuesday, 22 July 2014

Share Intent Example

Sending Simple Data to Other Apps

Sending and receiving data between applications with intents is most commonly used for social sharing of content. Intents allow users to share information quickly and easily, using their favorite applications.

NOTE: The best way to add a share action item to an ActionBar is to use ShareActionProvider, which became available in API level 14. ShareActionProvider is discussed in the lesson about Adding an Easy Share Action.

Send Text Content
The most straightforward and common use of the ACTION_SEND action is sending text content from one activity to another. For example, the built-in Browser app can share the URL of the currently-displayed page as text with any application. This is useful for sharing an article or website with friends via email or social networking. Here is the code to implement this type of sharing:

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);

If there's an installed application with a filter that matches ACTION_SEND and MIME type text/plain, the Android system will run it; if more than one application matches, the system displays a disambiguation dialog (a "chooser") that allows the user to choose an app. 

 However, if you call Intent.createChooser(), passing it your Intent object, it returns a version of your intent that will always display the chooser. This has some advantages:

  • Even if the user has previously selected a default action for this intent, the chooser will still be displayed.
  • If no applications match, Android displays a system message.
  • You can specify a title for the chooser dialog.

Example Application:
Screen Shots:



main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="56dp"
        android:onClick="share"
        android:text="Share" />

</RelativeLayout>

MainActivity.java:
package com.ram.shareintent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity
{

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

    public void share(View view)
    {
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
        sendIntent.setType("text/plain");
       startActivity(Intent.createChooser(sendIntent, "Share text to..."));
    }
}