[英]How to call Activity method in an AsyncTask
I would like to call an Activity
method after the onPostExecute
of my AsyncTask
. 我想在
AsyncTask
的onPostExecute
之后调用Activity
方法。 Do you know how I can do that? 你知道我该怎么做吗?
I want to call in the sendSMS(String phoneNumber, String message)
method in the onPostExecute
. 我想在调用
sendSMS(String phoneNumber, String message)
的方法onPostExecute
。
One way is to pass an instance of the Activity
through PostTask
constructor, something like: 一种方法是通过
PostTask
构造函数传递Activity
的实例,例如:
private class PostTask extends AsyncTask<String, Integer, String>
{
private AsyncBigCalculActivity activity;
public PostTask(AsyncBigCalculActivity activity)
{
this.activity = activity;
}
// ...
}
and on creating the PostTask
instance, pass the activity instance: 在创建
PostTask
实例时,传递活动实例:
new PostTask(this).execute();
Now you can invoke sendSMS()
from within PostTask
, like: 现在,您可以从
PostTask
内调用sendSMS()
,例如:
activty.sendSMS(...);
Also note that if you are defining the PostTask
as a private class inside the activty, then you can invoke sendSMS()
like: 另请注意,如果您将
PostTask
定义为PostTask
内部的私有类,则可以像以下方式调用sendSMS()
:
AsyncBigCalculActivity.this.sendSMS(...);
Add a constructor and a global variable to your AsyncTask like this: 像这样向您的AsyncTask添加一个构造函数和一个全局变量:
AsyncBigCalculActivity mActivity;
public PostTask(AsyncBigCalculActivity a) {
mActivity = a;
}
Then simply use mActivity.sendSMS("test", "test")
when you need it. 然后在需要时只需使用
mActivity.sendSMS("test", "test")
。
However, you should really have methods like sendSMS()
in a utility class. 但是,您实际上应该在实用程序类中具有
sendSMS()
类的方法。
If your AsyncTask
is an inner class of your Activity
then you should be able to call the Activity
method from your onPostExecute()
. 如果您的
AsyncTask
是Activity
的内部类,那么您应该能够从onPostExecute()
调用Activity
方法。 Otherwise, you can send the Context
to a constructor of your AsyncTask
and uses that to call the method 否则,您可以将
Context
发送到AsyncTask
的构造函数,并使用该构造函数调用该方法
You can create a CallBack using an interface. 您可以使用界面创建回调。 This way you can use your
AsyncTask
with any activity. 这样,您可以将
AsyncTask
与任何活动一起使用。 (Loosely coupled code) (松耦合代码)
1) Create a Callback 1)建立回呼
interface MyAsyncTaskCallBack{
public void doStuff(String arg1,String arg2);
}
2) Initialize the callback in your AsyncTask 2)在您的AsyncTask中初始化回调
private class MyTask extends AsyncTask<String, Void, Void>
{
private MyAsyncTaskCallBackactivity callback;
public MyTask(MyAsyncTaskCallBackactivity callback)
{
this.callback = callback;
}
//Call callback.doStuff(....phonenum, ....message); in your postExecute
}
3) Implement the Callback in your Activity and override doStuff() method 3)在您的Activity中实现回调并覆盖doStuff()方法
public YourActivity extends AppCompatActivity implements MyAsyncTaskCallBack{
// Your Activity code
// new MyTask(this).execute("phonenum","msg"); //<--- This is how you run AsyncTask
private void sendMessage(String num, String msg){
// send msg logic
}
@Override
public void doStuff(String arg1,String arg2){
sendMessage(arg1,arg2); // invoke activity method
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.