非活动类中的onActivityResult?

时间:2012-10-03 07:10:28

标签: android facebook android-activity

我正在尝试在Android中实现facebook登录和墙贴,所以我创建了一个处理所有内容的非活动类。我看到他们使用这种方法的所有这些例子 - onActivityResult但我不知道我是否必须使用它或为什么它如此重要。只要我没有在手机上安装facebook应用程序,代码就可以在没有它的情况下工作,我想知道onActivityResult是否与它有任何关系。 PS:我很确定我核心地生成了哈希密钥。谢谢。 :)

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;
import com.infobest.praiser.R;
import com.infobest.praiser.utils.Constants;

/**
 * Functionality for facebook sharing
 * 
 * @author oana_balaceanu
 * 
 */
public class ShareFacebook
{
    private static final String[] PERMISSIONS = new String[] {"publish_stream"};

    private Facebook facebook;
    private String messageToPost;
    private Context ctx;

    public ShareFacebook(String messageToPost, Context ctx)
    {
        this.messageToPost = messageToPost;
        this.ctx = ctx;

    }

    public boolean saveCredentials(Facebook facebook)
    {
        Editor editor = ctx.getSharedPreferences(Constants.KEY, Context.MODE_PRIVATE).edit();
        editor.putString(Constants.TOKEN, facebook.getAccessToken());
        editor.putLong(Constants.EXPIRES, facebook.getAccessExpires());
        return editor.commit();
    }

    public boolean restoreCredentials(Facebook facebook)
    {
        SharedPreferences sharedPreferences = ctx.getSharedPreferences(Constants.KEY,
            Context.MODE_PRIVATE);
        facebook.setAccessToken(sharedPreferences.getString(Constants.TOKEN, null));
        facebook.setAccessExpires(sharedPreferences.getLong(Constants.EXPIRES, 0));
        return facebook.isSessionValid();
    }

    public void share()
    {
        facebook = new Facebook(Constants.APP_ID);
        restoreCredentials(facebook);

        if (!facebook.isSessionValid())
        {
            loginAndPostToWall();
        }
        else
        {
            postToWall(messageToPost);
        }
    }


    public void loginAndPostToWall()
    {
        facebook.authorize((Activity) ctx, PERMISSIONS,
            (DialogListener) new LoginDialogListener());
    }

    public void postToWall(String message)
    {
        FacebookPoster fp = new FacebookPoster();
        fp.execute(message, null, null);
    }

    private class FacebookPoster extends AsyncTask<String, Object, Object>
    {

        @Override
        protected Object doInBackground(String... message)
        {
            Bundle parameters = new Bundle();
            parameters.putString("message", message[0]);
            parameters.putString("link", ctx.getResources().getString(R.string.rateLink));
            parameters.putString("picture", ctx.getResources().getString(R.string.linkIconPicture));
            try
            {
                facebook.request("me");
                String response = facebook.request("me/feed", parameters, "POST");
                Log.d("Tests", "got response: " + response);
                if (response == null || response.equals("") || response.equals("false"))
                {
                    return ctx.getResources().getString(R.string.facebookError);
                }
                else
                {
                    return ctx.getResources().getString(R.string.facebookSuccess);

                }
            }
            catch (Exception e)
            {
                Log.d("ShareOnFacebook", e.toString());
                return ctx.getResources().getString(R.string.facebookError);
            }
        }

        @Override
        protected void onPostExecute(Object result)
        {

            super.onPostExecute(result);
            showToast(result);
        }


    }


    class LoginDialogListener implements DialogListener
    {
        public void onComplete(Bundle values)
        {
            saveCredentials(facebook);
            if (messageToPost != null)
            {
                postToWall(messageToPost);
            }

        }

        public void onFacebookError(FacebookError error)
        {
            showToast(ctx.getResources().getString(R.string.facebookError));

        }

        public void onError(DialogError error)
        {
            showToast(ctx.getResources().getString(R.string.facebookError));

        }

        public void onCancel()
        {
            showToast(ctx.getResources().getString(R.string.facebookCancel));

        }
    }

    private void showToast(Object message)
    {
        Toast.makeText(ctx, message.toString(), Toast.LENGTH_SHORT).show();
    }


}

1 个答案:

答案 0 :(得分:0)

如果您在已安装Facebook应用的模拟器或智能手机上测试您的应用,则登录将因此失败。