-2

I have a GallecticaView class. I'm getting imageUrl and clickUrl after parsing in this class. I have to perform click operation in onPostExecute method. I have to open clickUrl be clicking on image which I'm loading using ImageLoader in onPostExecute. How to perform it successfully?

my GallecticaView class:

public class GallecticaView extends ImageView {

public GallecticaView(Context context){
    super(context);

    initIV(context);
}

public GallecticaView(Context ctx, AttributeSet set) {
    super(ctx, set);

    initIV(ctx);
}

public GallecticaView(Context ctx, AttributeSet set, int defStyleAttr) {
    super(ctx, set, defStyleAttr);

    initIV(ctx);
}

public GallecticaView(Context ctx, AttributeSet set, int defStyleAttr, int defStyleRes) {
    super(ctx, set, defStyleAttr);

    initIV(ctx);
}

private static String PREF_NAME = "gallectica_pref_adstuck";
private static SharedPreferences prefs;

private void initIV(final Context ctx) {

    setMinimumHeight(64);
    setMaxHeight(64);
    setAdjustViewBounds(true);
    setScaleType(ScaleType.CENTER_INSIDE);
    setPadding(0,0,0,0);

    new AsyncTask<Void, Void, Boolean>() {
        private String developer_public_key;
        private String bannerUrl, clickUrl;

        protected void onPreExecute() {
            super.onPreExecute();
            prefs = ctx.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
            prefs.edit().putString("android_id", Settings.Secure.getString(ctx.getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID)).commit();

            developer_public_key = prefs.getString("developer_public_key", "23/89533a6f4248873b08ce52ce680f29e7");
        }

        protected Boolean doInBackground(Void... arg0) {
            String json = "";
            HttpURLConnection connection = null;
            InputStream is = null;

            ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("aff_id", prefs.getString("android_id", "")));
            params.add(new BasicNameValuePair("developer_public_key", developer_public_key));


            try {
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost("http://a.nextput.com/api/single-offer/" + developer_public_key + "/a");//YOUR URL  ?aff_id

                HttpResponse httpResponse = httpClient.execute(httpPost);
                json = EntityUtils.toString(httpResponse.getEntity());

                JSONObject jObj = new JSONObject(json);
                boolean isSuccess = jObj.getBoolean("success");
                System.out.println("success : " + isSuccess);

                /* JSONObject jsonObject = new JSONObject(json);
                   boolean state = jsonObject.getBoolean("success");*/

                JSONObject jo = jObj.getJSONObject("offer");

                bannerUrl = jo.getString("banner_url");
                clickUrl = jo.getString("click_url");


                return isSuccess;
            } catch (Exception e) {
                Log.e("JSON Parser", "Error parsing data " + e.toString());
            }

            return false;
        }

        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);

            ImageLoader.getInstance().displayImage(bannerUrl, GallecticaView.this);

            ImageView img = (ImageView) findViewById(R.id.bannerAds);
            img.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {

                }
            });


        }
    }.execute();

}
}

My xml file in which I created imageview. bannerUrl is opening in this imageview:

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Incent"/>


<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="NonIncent"
    android:layout_toRightOf="@+id/button1"/>


<com.example.a7.samplelibrary.GallecticaView
    android:id="@+id/bannerAds"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="visible"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />
Pravin Divraniya
  • 3,642
  • 2
  • 26
  • 45
himanshu.tiwari
  • 71
  • 1
  • 12

1 Answers1

0

Take a look on how to open your url in external browser : How can I open a URL in Android's web browser from my application?

Looks like this is what you need:

img.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent browserIntent = new Intent(Intent.ACTION_VIEW,  Uri.parse(clickUrl));
                Context context = getContext();
                if (context instanceof Activity) {
                    ((Activity) context).startActivity(browserIntent);
                }                
            }
        });
Community
  • 1
  • 1
amukhachov
  • 5,566
  • 1
  • 37
  • 55
  • This is a class, not an activity. So it is giving error 'cannot resolve method 'startActivity''. That's why I'm confused how to perform click in this class. please help – himanshu.tiwari Feb 17 '16 at 13:11
  • @himanshu.tiwari You can get `Activity` by casting `View`'s `Context`. Answer edited – amukhachov Feb 17 '16 at 13:48