Android 从库异步任务返回意图
我正试图跟上,但遇到了一些麻烦。我正在尝试创建一个调查SDK,开发人员可以将它插入到他们的应用程序中,这样他们就可以调查他们的用户。它在我的应用程序中非常有效,但当我提取它并创建库时,我遇到了一些麻烦 理想情况下,它会做的是检查一个调查,然后如果一个调查是可用的,则返回一个意图,客户端可以在他们想要的任何UI中调用该意图 我想我在通过接口/委托返回意图时被绊倒了 以下是我的客户代码(去掉不重要的部分): 我的库代码:Android 从库异步任务返回意图,android,android-intent,android-asynctask,Android,Android Intent,Android Asynctask,我正试图跟上,但遇到了一些麻烦。我正在尝试创建一个调查SDK,开发人员可以将它插入到他们的应用程序中,这样他们就可以调查他们的用户。它在我的应用程序中非常有效,但当我提取它并创建库时,我遇到了一些麻烦 理想情况下,它会做的是检查一个调查,然后如果一个调查是可用的,则返回一个意图,客户端可以在他们想要的任何UI中调用该意图 我想我在通过接口/委托返回意图时被绊倒了 以下是我的客户代码(去掉不重要的部分): 我的库代码: public void checkSurvey(Context context
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 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);
}