public class Tab1Fragment extends Fragment {
/** (non-Javadoc)
* @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
*/
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
// We have different layouts, and in one of them this
// fragment's containing frame doesn't exist. The fragment
// may still be created from its saved state, but there is
// no reason to try to create its view hierarchy because it
// won't be displayed. Note this is not needed -- we could
// just run the code below, where we would create and return
// the view hierarchy; it would just never be used.
return null;
}
return (LinearLayout)inflater.inflate(R.layout.tab_frag1_layout, container, false);
}
}
อันนีเโค้ดที่ extend Activity Code (Android-Java)
package com.travel;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import com.travel.AsyncFacebookRunner.RequestListener;
import com.travel.SessionEvents.AuthListener;
import com.travel.SessionEvents.LogoutListener;
import android.os.Bundle;
import android.os.StrictMode;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class show_menu extends Activity implements OnClickListener {
private static final String tag = "show_menu";
private LoginButton mLoginButton;
String name;
String id;
String idName;
private Facebook mFacebook;
private AsyncFacebookRunner mAsyncRunner;
private TextView txtLogin;
private TextView txtLoginID;
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(tag, getResources().getString(R.string.CREATING_VIEW));
mFacebook = new Facebook(getResources().getString(R.string.FACEBOOK_ID_TEST));
setContentView(R.layout.show_menu);
// Permission StrictMode
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();
String status = pref.getString("status", "");
// Intent intent= getIntent();
// String status = intent.getStringExtra("status");
// String MemberID = intent.getStringExtra("MemberID");
mLoginButton = (LoginButton) this.findViewById(R.id.login);
txtLogin = (TextView) this.findViewById(R.id.txtShowName);
txtLoginID = (TextView) this.findViewById(R.id.txtLoginID);
// txtLogin.setText(status);
if(status == "userLogin"){
txtLogin.setText(status);
//txtLoginID.setText(MemberID);
editor.putString("status", status.toString().trim());
editor.commit();
}
else if(status == "facebookLogin"){
txtLogin.setText(status);
//txtLoginID.setText(strMemberID);
editor.putString("status", status.toString().trim());
editor.commit();
mAsyncRunner = new AsyncFacebookRunner(mFacebook);
mAsyncRunner.request("me", new SampleRequestListener());
}
//
//mAsyncRunner = new AsyncFacebookRunner(mFacebook);
//mAsyncRunner.request("me", new SampleRequestListener());
SessionStore.restore(mFacebook, this);
SessionEvents.addAuthListener(new SampleAuthListener());
SessionEvents.addLogoutListener(new SampleLogoutListener());
mLoginButton.init(this, mFacebook);
// btnDetailMember
Button btnDetailMember = (Button) findViewById(R.id.btnDetailMember);
btnDetailMember.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent newActivity = new Intent(show_menu.this,DetailMember.class);
finish();
startActivity(newActivity);
}
});
// btnLogOut
Button btnLogout = (Button) findViewById(R.id.btnLogout);
btnLogout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();
String status = pref.getString("status", "");
if(status == "userLogin"){
editor.clear();
editor.commit();
Intent newActivity = new Intent(show_menu.this,Main.class);
finish();
startActivity(newActivity);
}
else{
mLoginButton.performClick();
}
}
});
// btnMenuAdmin
Button btnAdmin = (Button) findViewById(R.id.btnAdmin);
btnAdmin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent newActivity = new Intent(show_menu.this,Main_tab.class);
finish();
startActivity(newActivity);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
mFacebook.authorizeCallback(requestCode, resultCode, data);
}
//
public class SampleAuthListener implements AuthListener
{
@Override
public void onAuthSucceed()
{
// Intent intent = new Intent(Main.this,show_menu.class);
// startActivity(intent);
}
@Override
public void onAuthFail(String error)
{
}
}
public class SampleLogoutListener implements LogoutListener
{
@Override
public void onLogoutBegin()
{
}
@Override
public void onLogoutFinish()
{
Intent intent = new Intent(show_menu.this,Main.class);
finish();
startActivity(intent);
}
}
public class SampleRequestListener extends BaseRequestListener
{
@Override
public void onComplete(final String response, final Object state)
{
try
{
// process the response here: executed in background thread
Log.d("Facebook-Example", "Response: " + response.toString());
JSONObject json = Util.parseJson(response);
name = json.getString("name");
id = json.getString("id");
// then post the processed result back to the UI thread
// if we do not do this, an runtime exception will be generated
// e.g. "CalledFromWrongThreadException: Only the original
// thread that created a view hierarchy can touch its views."
show_menu.this.runOnUiThread(new Runnable()
{
@Override
public void run()
{
//txtLogin.setText("Hello there, " + name);
txtLoginID.setText(id);
}
});
}
catch (JSONException e)
{
Log.w("Facebook-Example", "JSON Error in response");
}
catch (FacebookError e)
{
Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
}
}
}
public class SampleUploadListener extends BaseRequestListener
{
@Override
public void onComplete(final String response, final Object state)
{
try
{
// process the response here: (executed in background thread)
Log.d("Facebook-Example", "Response: " + response.toString());
JSONObject json = Util.parseJson(response);
final String src = json.getString("src");
// then post the processed result back to the UI thread
// if we do not do this, an runtime exception will be generated
// e.g. "CalledFromWrongThreadException: Only the original
// thread that created a view hierarchy can touch its views."
show_menu.this.runOnUiThread(new Runnable()
{
@Override
public void run()
{
}
});
}
catch (JSONException e)
{
Log.w("Facebook-Example", "JSON Error in response");
}
catch (FacebookError e)
{
Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
}
}
}
public class WallPostRequestListener extends BaseRequestListener
{
@Override
public void onComplete(final String response, final Object state)
{
Log.d("Facebook-Example", "Got response: " + response);
String message = "<empty>";
try
{
JSONObject json = Util.parseJson(response);
message = json.getString("message");
}
catch (JSONException e)
{
Log.w("Facebook-Example", "JSON Error in response");
}
catch (FacebookError e)
{
Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
}
final String text = "Your Wall Post: " + message;
show_menu.this.runOnUiThread(new Runnable()
{
@Override
public void run()
{
}
});
}
}
public class WallPostDeleteListener extends BaseRequestListener
{
@Override
public void onComplete(final String response, final Object state)
{
if (response.equals("true"))
{
Log.d("Facebook-Example", "Successfully deleted wall post");
show_menu.this.runOnUiThread(new Runnable()
{
@Override
public void run()
{
}
});
}
else
{
Log.d("Facebook-Example", "Could not delete wall post");
}
}
}
public class SampleDialogListener extends BaseDialogListener
{
@Override
public void onComplete(Bundle values)
{
final String postId = values.getString("post_id");
if (postId != null)
{
Log.d("Facebook-Example", "Dialog Success! post_id=" + postId);
mAsyncRunner.request(postId, new WallPostRequestListener());
}
else
{
Log.d("Facebook-Example", "No wall post made");
}
}
}
/*
* (non-Javadoc)
*
* @see android.view.View.OnClickListener#onClick(android.view.View)
*/
public void onClick(View v)
{
if (v == mLoginButton)
{
}
}
public String getHttpPost(String url,List<NameValuePair> params) {
StringBuilder str = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
HttpResponse response = client.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) { // Status OK
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
str.append(line);
}
} else {
Log.e("Log", "Failed to download result..");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}
public void logoutFromFacebook() {
mAsyncRunner.logout(this, new RequestListener() {
@Override
public void onComplete(String response, Object state) {
Log.d("Logout from Facebook", response);
if (Boolean.parseBoolean(response) == true) {
runOnUiThread(new Runnable() {
@Override
public void run() {
}
});
}
}
@Override
public void onIOException(IOException e, Object state) {
// TODO Auto-generated method stub
}
@Override
public void onFileNotFoundException(FileNotFoundException e,
Object state) {
// TODO Auto-generated method stub
}
@Override
public void onMalformedURLException(MalformedURLException e,
Object state) {
// TODO Auto-generated method stub
}
@Override
public void onFacebookError(FacebookError e, Object state) {
// TODO Auto-generated method stub
}
});
}
}