我的回调方法从不使用接口调用

My callback method is never called using an interface

我正在尝试实现一个 callback 方法,以便在 Thread 完成时调用它的工作。 我使用的是 interface 方法而不是 Handler 方法。

我有一个主要的 UI Thread,它是 onCreate(Bundle) 方法,还有一个 Thread 我从 onCreate(Bundle) 方法中调用。

(仅发布相关代码)。

MainActivity.java:

public class MainActivity extends AppCompatActivity implements GetDataFromTheWebThreadCallback
{
    public static GetDataFromTheWebThread getDataFromTheWebThread;
    private GetDataFromTheWebEventNotifier eventNotifier;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.eventNotifier = new GetDataFromTheWebEventNotifier(MainActivity.this);

        // The thread that will search the web for data
        this.getDataFromTheWebThread = new GetDataFromTheWebThread();
        getDataFromTheWebThread.start();
     }

        @Override
        public void finishParsing() // The callback method that never called
        {
            Toast.makeText(MainActivity.this,"Callback Method Called",Toast.LENGTH_LONG).show();
            Log.d("Callback:", "Callback Method Called");
        }
}

GetDataFromTheWebEventNotifier.java:

public class GetDataFromTheWebEventNotifier
{
    private GetDataFromTheWebThreadCallback callbackInterface;

    public GetDataFromTheWebEventNotifier(GetDataFromTheWebThreadCallback callbackInterface)
    {
        this.callbackInterface = callbackInterface;
    }

    public void onEvent()
    {
            this.callbackInterface.finishParsing();
    }
}

GetDataFromTheWebThreadCallback.java:

public interface GetDataFromTheWebThreadCallback
{
    void finishParsing(); // The method i wish to invoke when certain event will happen
}

GetDataFromTheWebThread.java:

public class GetDataFromTheWebThread extends Thread
{
    public static boolean isFinished = false; // False - the thread is still running. True - the thread is dead

    @Override
    public void run()
    {
        GetDataFromTheWebThread.isFinished = false;
        try
        {
            // Some internet computations...
            Thread.sleep(100);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        GetDataFromTheWebThread.isFinished = true;
    }
}

那我的callback怎么了?

你从不打电话给 onEvent()。您的通知程序是否应该监视 isFinished 变量或其他内容?

至于你的 ThreadClass,有一个带回调的构造函数:

public class GetDataFromTheWebThread extends Thread {
    public static boolean isFinished = false; // False - the thread is still running. True - the thread is dead
    private GetDataFromTheWebThreadCallback mCallback;

    public GetDataFromTheWebThread(GetDataFromTheWebThreadCallback c) {
      mCallback = c;
    }

    @Override
    public void run() {
      GetDataFromTheWebThread.isFinished = false;
      try {
        // Some internet computations...
        Thread.sleep(100);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      GetDataFromTheWebThread.isFinished = true;
      if (mCallback !- null) {
        mCallback.finishParsing();
      }
    }
}

至于您的 Activity,只需在创建线程时传递回调即可:

this.getDataFromTheWebThread = new GetDataFromTheWebThread(this);

以及 :

    @Override
    public void finishParsing()  {
      // You know that this function is called from a background Thread.
      // Therefore from here, run what you have to do on the UI Thread
      runOnUiThread(new Runnable() {
        @Override
        public void run() {
          Toast.makeText(MainActivity.this,"Callback Method Called",Toast.LENGTH_LONG).show();
          Log.d("Callback:", "Callback Method Called");
        }});

    }

实际上你并没有调用onEvent()。并检查 AsyncTask.