0

I'm trying to follow how to get the result of onpostexecute to main activitiy but am running into some trouble. I'm trying to create a survey SDK that developers can plug into their apps so they can survey their users. It works great in my app but as I'm extracting it and creating a library I'm running into some trouble.

Ideally what it would do is check for a survey, then if a survey is available return an intent which the client can call in whatever UI they want.

I think I'm getting tripped up in returning the intent via an interface/delegate.

Here's my client code (with unimportant pieces stripped out):

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

My library code:

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

And my interface:

public interface SurveyMeResponse {
    void takeSurvey(Intent intent);
}

Any idea how I should adjust my code so that I can pass back an intent?

Thanks in advance!

Community
  • 1
  • 1
Tom Hammond
  • 5,842
  • 12
  • 52
  • 95

0 Answers0