【问题标题】:My callback method is never called using an interface我的回调方法永远不会使用接口调用
【发布时间】:2016-08-09 05:37:20
【问题描述】:

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

我有一个主要的 UI ThreadonCreate(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 怎么了?

【问题讨论】:

    标签: android multithreading interface callback ui-thread


    【解决方案1】:

    至于你的 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();
          }
        }
    }
    

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

    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");
            }});
    
        }
    

    【讨论】:

    • 但是那样就不是真正的callback。我想在onEvent() 方法上检查GetDataFromTheWebThread.isFinished = true; 是否在我知道Thread 完成时不调用callback。哦,它给了RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
    • @God 你是什么意思它不是真正的回调?回调是为了在事件发生时调用一段特殊的代码,例如在您的情况下线程完成。
    • 好吧,它仍然会生成一个RunTimeException
    • 至于异常,请参阅我的编辑 (runOnUIThread()),它将强制从 UIThread 运行回调调用,从而防止您的 RunTimeException
    • 或者我可以在run()方法的开头使用Looper.prepare();
    【解决方案2】:

    你永远不会打电话给onEvent()。你的通知器应该在看isFinished 变量吗?

    【讨论】:

    • 因为如果Thread 完成了又名GetDataFromTheWebThread.isFinished = true;,我想打电话给onEvent()
    • Is your notifier supposed to be watching the isFinished variable or something? 我不知道。我只想让他打电话给onEvent(),只要Thread“死”,它就会调用callback方法。
    【解决方案3】:

    其实你并没有调用 onEvent()。并检查AsyncTask

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-08
    • 2014-06-22
    • 1970-01-01
    相关资源
    最近更新 更多