如何在类中执行onClick并在Android中打开URL?

时间:2016-02-17 13:02:20

标签: android json android-fragments button android-intent

我有一个GallecticaView课程。我在这个类中解析后得到imageUrl和clickUrl。我必须在onPostExecute方法中执行点击操作。我必须打开clickUrl点击我在onPostExecute中使用ImageLoader加载的图片。如何成功执行?

我的GallecticaView课程:

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

}
}

我在其中创建了imageview的xml文件。 bannerUrl正在此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" />

1 个答案:

答案 0 :(得分:0)

看看如何在外部浏览器中打开您的网址: How can I open a URL in Android's web browser from my application?

看起来这就是你需要的:

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