将Facebook变量传递给Tab android

时间:2015-05-16 12:27:46

标签: java javascript android facebook facebook-graph-api

我想知道是否有方法将FBUSER和FBID传递给我的每个标签栏,并在每个网页视图中通过将它们传递到网址中来使用它们,因为我需要在我的网页中使用它们网络视图。 这是代码:

public class SampleFragment extends Fragment {

    private static final String ARG_POSITION = "position";
    private WebView myWebView;
    private String LOG_TAG = "AndroidWebViewActivity";

    private int position;


    public static SampleFragment newInstance(int position) {
        SampleFragment f = new SampleFragment();
        Bundle b = new Bundle();
        b.putInt(ARG_POSITION, position);
        f.setArguments(b);
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        position = getArguments().getInt(ARG_POSITION);
        View rootView = inflater.inflate(page, container, false);
        FloatingActionButton fab = (FloatingActionButton) rootView.findViewById(R.id.fabButton);
        final ProgressBarCircular progressBarCircular = (ProgressBarCircular) rootView.findViewById(R.id.progress);

        Log.i("ADebugTag", "Value: ");


        final WebView webView = (WebView) rootView.findViewById(R.id.webView);



        webView.getSettings().setBuiltInZoomControls(false);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setAllowContentAccess(true);

        webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
        webView.getSettings().setAllowFileAccessFromFileURLs(true);
        webView.getSettings().setAllowContentAccess(true);
        webView.getSettings().setAppCacheEnabled(true);


        webView.getSettings().setSupportZoom(false);
        webView.setInitialScale(0);

        LoginButton loginButton = (LoginButton) rootView.findViewById(R.id.login_button);
        loginButton.setReadPermissions("user_friends");
        // If using in a fragment
        loginButton.setFragment(this);
        // Other app specific specialization

        CallbackManager callbackManager = CallbackManager.Factory.create();
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                GraphRequest request = GraphRequest.newMeRequest(
                        loginResult.getAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(
                                    JSONObject object,
                                    GraphResponse response) {

                                Profile profile = Profile.getCurrentProfile();
                                String firstName = profile.getFirstName();
                                String a = "a";

                                Log.i("ADebugTag", "Value: " + firstName);

                            }
                        });

                Bundle parameters = new Bundle();
                parameters.putString("fields", "id,name,email");
                request.setParameters(parameters);
                request.executeAsync();

            }

            @Override
            public void onCancel() {
                Log.i("ADebugTag", "BANANA ");
            }

            @Override
            public void onError(FacebookException exception) {
                Log.i("ADebugTag", "BOOH ");
            }
        });





        switch (position) {
            case 0:

                fab.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        webView.loadUrl("file:///android_asset/eventiresponsive.html");
                    }
                });

            webView.loadUrl("file:///android_asset/eventiresponsive.html");

                webView.setWebViewClient(new WebViewClient() {

                    public void onPageFinished(WebView view, String url) {
                        progressBarCircular.setVisibility(View.GONE);
                    }
                });
                loginButton.setVisibility(View.GONE);


                break;
            case 1:
                fab.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        webView.loadUrl("file:///android_asset/fotostream.html");
                    }
                });


                loginButton.setVisibility(View.GONE);
                webView.loadUrl("file:///android_asset/fotostream.html");

                webView.setWebViewClient(new WebViewClient() {

                    public void onPageFinished(WebView view, String url) {
                        progressBarCircular.setVisibility(View.GONE);
                    }
                });


                break;
            case 2:
                fab.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        webView.loadUrl("file:///android_asset/eventiresponsive.html");
                    }
                });


                webView.setWebViewClient(new WebViewClient() {

                    public void onPageFinished(WebView view, String url) {
                        progressBarCircular.setVisibility(View.GONE);
                    }
                });

                loginButton.setVisibility(View.GONE);
                webView.loadUrl("http://www.google.com");

                break;
            case 3:
                fab.setVisibility(View.GONE);
                loginButton.setVisibility(View.VISIBLE);
                break;
        }

        return rootView;
    }
    @Override

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        CallbackManager callbackManager = CallbackManager.Factory.create();
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }

1 个答案:

答案 0 :(得分:0)

我知道简单地获取这些变量可能非常棘手,因为它们总是嵌套在回调方法中。

我所做的是将它们存储在SharedPreferences中,然后我可以轻松地将它们放在应用程序的另一部分中。

要编写更优雅,更重要的安全/高效代码,您可以查找事件总线(如Otto,EventBus)或Observables(RxJava),它们可以非常容易地异步发送这些对象!

希望我能帮助你:)。