Search code examples
androidfacebookandroid-asynctaskfacebook-android-sdk

Facebook authorize not working inside Android Asynctask or Thread


Working with facebook in Android. Sometimes my application is cashing in real time device when I tried to authorize Facebook in Android.not in emulator. I used the Android Facebook SDK. So I thought threading might stop that.First tried the asynctask

Activity act=this;

private class fbwork extends AsyncTask<Facebook, Integer, String>
{

    @Override
    protected String doInBackground(Facebook... para)
    {
        // TODO Auto-generated method stub
        Log.d(tagg, "Entered async");
            if(loginflag==0)
            {
                try
                {
                    para[0].authorize(act, PERMISSIONS, new LoginDialogListener());
                }catch(Exception ex)
                {
                    Log.d(tagg,ex.getMessage());
                }
                Log.d(tagg, tagg.toString());
            }
            else
            {

                try {
                    logout();
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }


        return "0";
    }

calling code:

new fbwork().execute(facebook);

produce error: Can't create handler inside thread that has not called Looper.Prepare()

So tried the normal threading way.

public void loginprocesure() throws MalformedURLException, IOException
{
    final Activity ac=this;
    if(loginflag==0)
    {
        new Thread(new Runnable() {

            public void run() {
                // TODO Auto-generated method stub
                facebook.authorize(ac, PERMISSIONS, new LoginDialogListener());
            }
        }).start();

    }
    else
    {

        logout();
    }
}

Again same result. any way to fix this!!!! How to stop that crashing of application in real device. Please help.


Solution

  • I faced the same issue.You must try to put this method in loop

     Looper.prepare();
     new fbwork().execute(facebook);
     Looper.loop();