Tuesday, December 11, 2012

Launch Facebook app from a specified page, using intent with ACTION_VIEW.

Last exercises "Start facbook app by startActivity(intent)" and "Check and request to install Facebook App" launch Facebook app from LoginActivity. Alternatively, we can launch Facebook app from a specified page, using using intent with ACTION_VIEW, and specified Uri.

Launch Facebook app from a specified page, using intent with ACTION_VIEW.


package com.example.androidstartfacebook;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity {
 
 Button btnStartFacebook;
 
 Spinner spAction;
 String[] facebookUris ={
   "facebook:/chat",
   "facebook:/events",
   "facebook:/friends",
   "facebook:/inbox",
   "facebook:/info",
   "facebook:/newsfeed",
   "facebook:/places",
   "facebook:/requests",
   "facebook:/wall"
 };
 

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  btnStartFacebook = (Button)findViewById(R.id.btnstartfacebook);
  btnStartFacebook.setOnClickListener(btnStartFacebookOnClickListener);
  
  spAction = (Spinner)findViewById(R.id.spaction);
  ArrayAdapter<String> adapter = new ArrayAdapter<String>(
    this, 
    android.R.layout.simple_spinner_item, 
    facebookUris);
  adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  spAction.setAdapter(adapter);
 }
 
 OnClickListener btnStartFacebookOnClickListener
 = new OnClickListener(){

  @Override
  public void onClick(View v) {
   
   String facebookPackageName = "com.facebook.katana";

   try {
    //check if facebook app installed
    getPackageManager().getApplicationInfo(facebookPackageName, 0);
    
    spAction.getSelectedItem();
    String action = (String)spAction.getSelectedItem();
    Toast.makeText(getApplicationContext(), action, Toast.LENGTH_LONG).show();
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(action));
    startActivity(intent);
    
   } catch (NameNotFoundException e) {
    // Didn't installed
    Toast.makeText(getApplicationContext(), "Facebook not found! INSTALL.", Toast.LENGTH_LONG).show();
    
    //Start Market to download and install Facebook App
    Uri uri = Uri.parse("market://details?id=" + facebookPackageName); 
    Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
    startActivity(intent);
   }
   
  }};

}


<LinearLayout 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=".MainActivity" 
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <Spinner
        android:id="@+id/spaction"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <Button 
        android:id="@+id/btnstartfacebook"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start facebook app" />

</LinearLayout>


download filesDownload the files.

1 comment:

U Zarni Moe Myint said...

hello
thank your post it is very helpful
but i am trying to show one fan page.
how can i do that.
thank