我正在尝试关注how to get the result of onpostexecute to main activitiy,但遇到了一些麻烦。我正在尝试创建一个调查SDK,开发人员可以插入他们的应用程序,以便他们可以调查他们的用户。它在我的应用程序中运行良好,但是当我正在提取它并创建一个库时,我遇到了一些麻烦。
理想情况下,它会检查调查,然后如果调查可用,则返回客户可以在他们想要的任何UI中调用的意图。
我想通过界面/代表返回意图时我会被绊倒。
这是我的客户端代码(删除了不重要的部分):
public class MainActivity extends Activity implements SurveyMeResponse {
public String DEVELOPER_ID = "f38de56f515014e8b7aa3102b1ed6df9";
private Button mTakeSurvey;
private Intent mIntent = new Intent();
@Override
protected void onResume() {
super.onResume();
new StartSurveyFragment().checkSurvey(getApplicationContext(), mTakeSurvey, mIntent, DEVELOPER_ID);
}
@Override
public void takeSurvey(Intent intent) {
// TODO Auto-generated method stub
mIntent = intent;
}
我的图书馆代码:
public void checkSurvey(Context context, Button button, Intent intent, String developerId) {
mDeveloperId = developerId;
new SurveyAvailable(context, button, intent).execute();
}
private class SurveyAvailable extends AsyncTask<Void, Void, Survey> {
private Context mContext;
private Button mButton;
private Intent mIntent;
SurveyMeResponse delegate = null;
private SurveyAvailable(Context context, Button button, Intent intent) {
this.mContext = context;
this.mButton = button;
this.mIntent = intent;
delegate = (SurveyMeResponse) mContext;
}
@Override
protected Survey doInBackground(Void... params) {
TelephonyManager telephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
String deviceId = telephonyManager.getDeviceId();
SharedPreferences sp = mContext.getSharedPreferences(
"SurveyMeSharedPreferences", mContext.MODE_PRIVATE);
String apiKey = sp.getString("user_auth_token", null);
if (apiKey == null) {
SharedPreferences.Editor editor = sp.edit();
try {
apiKey = SurveyMe.checkUser(deviceId);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
editor.putString("user_auth_token", apiKey);
editor.commit();
}
String user = sp.getString("user_auth_token", null);
Log.i("SurveyMe", "User is: " + user);
Survey survey = null;
try {
survey = new SurveyMe()
.getSurvey(user);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return survey;
}
@Override
protected void onPostExecute(Survey survey) {
if (survey == null) {
mButton.setVisibility(View.GONE);
Toast.makeText(mContext,
"There are no surveys currently availabile",
Toast.LENGTH_SHORT).show();
return;
}
if (survey != null) {
mButton.setVisibility(View.VISIBLE);
SurveyLab.get(mContext).addSurvey(survey);
mSurvey = SurveyLab.get(mContext)
.getSurvey(survey.getId());
mIntent = new Intent(mContext, TakeSurveyActivity.class);
mIntent.putExtra(TakeSurveyFragment.SURVEY_ID, survey.getId());
delegate.takeSurvey(mIntent);
}
}
}
我的界面:
public interface SurveyMeResponse {
void takeSurvey(Intent intent);
}
知道如何调整我的代码以便我可以传回一个意图吗?
提前致谢!