我有同样的问题。它使用广播接收器解决了。我是这样做的:
class TabsTask extends AsyncTask<String, Void, String> {
static final String BROADCAST_ACTION = "CALL_FUNCTION";
@Override protected String doInBackground(String... params) {
//some statements
}
protected void onPostExecute(String result) {
try {
Intent broadcast = new Intent();
broadcast.setAction(BROADCAST_ACTION);
mContext.sendBroadcast(broadcast);
} catch (Exception e) {
//some statement(s)
}
}
}
现在,在您的片段类中:
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
function_you_want_to_call();
}
};
public void onResume() {
//other statements
IntentFilter filter = new IntentFilter();
filter.addAction(TabsTask.BROADCAST_ACTION);
activity.registerReceiver(receiver, filter);
}
public void onPause() {
//other statements
activity.unregisterReceiver(receiver);
}