Wednesday, December 5, 2012

Start facbook app by startActivity(intent)

To start official Facebook for Android specifically, we can startActivity with intent, with PackageName of "com.facebook.katana", and ClassName of "com.facebook.katana.LoginActivity".

Start facbook app by startActivity(intent)


It's assumed Facebook app have been already installed.

package com.example.androidstartfacebook;

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

public class MainActivity extends Activity {
 
 Button btnStartFacebook;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  btnStartFacebook = (Button)findViewById(R.id.btnstartfacebook);
  
  btnStartFacebook.setOnClickListener(btnStartFacebookOnClickListener);
 }
 
 OnClickListener btnStartFacebookOnClickListener
 = new OnClickListener(){

  @Override
  public void onClick(View v) {
   Intent intent = new Intent("android.intent.category.LAUNCHER");
   String facebookPackageName = "com.facebook.katana";
   String facebookClassName = "com.facebook.katana.LoginActivity";
   intent.setClassName(facebookPackageName, facebookClassName);
   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" />
    <Button 
        android:id="@+id/btnstartfacebook"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start facebook app" />

</LinearLayout>



Next:
- Check and request to install Facebook App
- Launch Facebook app from a specified page, using intent with ACTION_VIEW.


No comments: