简体   繁体   中英

Android - Facebook login dialog and progress indicatior keeps popping out upon logging in

I have implemented Facebook capabilities in my Android running application. When the user presses on the Facebook ImageButton , it will start an authentication process (SSO). And then do a post to the user's facebook wall. Everything runs smoothly on the emulator. Able to post on wall and able to view.

However, while testing on a real device, one problem occur after user presses the ImageButton . The ProgressDialog keeps popping out and does not stop. User will need to close program by pressing the Home button on the device.

在此处输入图片说明

What could be the problem? I didn't changed any codes in the Facebook.java class

Adapter.java

ImageButton fbBtn = (ImageButton) view.findViewById(R.id.fb); 
    fbBtn.setOnClickListener(
            new Button.OnClickListener() {
                public void onClick(View v) 
                {
                    taskListener.doAuthentication();
                    taskListener.postToWall(data[position], text[position], name[position]); 
                    System.out.println(text[position]);
                    }
                }

        );

public static interface FBookTaskListener{
    public void doAuthentication();
    public void postToWall(String data, String text, String name);
}

Activity.java

protected class TheTask extends AsyncTask<Void, Void, MyResultClass >{

    protected void onPreExecute() {
        dialog = ProgressDialog.show(Activity.this, "Retrieving Information", "Please wait for a few seconds...", true, false);
        dialog.setCancelable(true);
    }

    protected MyResultClass doInBackground(Void... params) {
        searchContent();
        MyResultClass result = new MyResultClass();
        result.mStrings = mStrings;
        result.dStrings = dStrings;
        result.date = date;
        result.name = name;
        return result;
    }   
    @Override
    protected void onPostExecute(MyResultClass result) {            
        dStrings = result.dStrings;
        mStrings = result.mStrings;
        date = result.date;
        name = result.name;
        LazyAdapter adapter = new Adapter(Activity.this, mStrings, dStrings, name);
        list.setAdapter(adapter);

        adapter.setTaskListener(new FBookTaskListener(){
            public void doAuthentication()
            {
                    // here all your FB authentication related stuff.

                mPrefs = getPreferences(MODE_PRIVATE);
                String access_token = mPrefs.getString("access_token", null);
                long expires = mPrefs.getLong("access_expires", 0);
                if(access_token != null) {
                    facebook.setAccessToken(access_token);
                }
                if(expires != 0) {
                    facebook.setAccessExpires(expires);
                }

                /*
                 * Only call authorize if the access_token has expired.
                 */
                if(!facebook.isSessionValid()) {

                facebook.authorize(Activity.this, new String[] {"publish_stream", "offline_access", "read_stream"}, new DialogListener() {
                    @Override
                    public void onComplete(Bundle values) 
                    {
                        SharedPreferences.Editor editor = mPrefs.edit();
                        editor.putString("access_token", facebook.getAccessToken());
                        editor.putLong("access_expires", facebook.getAccessExpires());
                        editor.commit();
                    }

                    @Override
                    public void onFacebookError(FacebookError e) {
                        Log.d("FACEBOOK ERROR","FB ERROR. MSG: "+e.getMessage()+", CAUSE: "+e.getCause());
                    }

                    @Override
                    public void onError(DialogError e) {
                        Log.e("ERROR","AUTH ERROR. MSG: "+e.getMessage()+", CAUSE: "+e.getCause());
                    }

                    @Override
                    public void onCancel() {
                        Log.d("CANCELLED","AUTH CANCELLED");
                    }
                });
            }

            }

            @Override
            public void postToWall(String data, String text, String name) {
                postToFacebook(data, text, name);

            }

        });

        dialog.dismiss();
    }       
}

Add this code in ur activity

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    facebook.authorizeCallback(requestCode, resultCode, data);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM