如何在WebView中单击按钮获取值?

时间:2017-08-18 00:51:43

标签: javascript android webview appsflyer

我有一个原生的Android应用程序进入WebView进行结帐过程。我正在实施appflyer以通过应用程序跟踪收入。如何检测页面上单击了哪个按钮,以及项目价格是多少?

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.appsflyer.AFInAppEventParameterName;
import com.appsflyer.AFInAppEventType;
import com.appsflyer.AppsFlyerLib;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class EventActivity extends AppCompatActivity {

    ProgressDialog pDialog;
    Boolean redirecting = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event);

        String appsFlyerUID = AppCore.getInstance(getApplicationContext()).getAppsFlyerUID();
        String idfa = AppCore.getInstance(getApplicationContext()).getMixpanelAdvertisingIdentifier();

        Intent intent = getIntent();

        try {
            JSONObject event = new JSONObject(intent.getStringExtra("event"));

            String eventId = event.getString("id");
            String title = event.getString("title");
            String startsAt = event.getString("starts_at");
            String venue = event.getString("venue");
            String city = event.getString("city");
            String state = event.getString("state");

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
            Date newDate = format.parse(startsAt);

            format = new SimpleDateFormat("EEEE, MMMM d @ h:mm a");
            String date = format.format(newDate);

            ((TextView)findViewById(R.id.event_title)).setText(title);
            ((TextView)findViewById(R.id.event_date)).setText(date);
            ((TextView)findViewById(R.id.event_venue)).setText(venue + " - " + city + ", " + state);

            String url = "http://tlapi.ticketliquidator.com/event_map/" + eventId + "?utm_campaign=" + idfa + "&utm_term=" + appsFlyerUID;


            Map<String, Object> eventValue = new HashMap<String, Object>();
            AppsFlyerLib.getInstance().trackEvent(getApplicationContext(), "View Event " + eventId,eventValue);


            final WebView wv = (WebView) findViewById(R.id.event_webview);
            wv.getSettings().setJavaScriptEnabled(true);
            final Activity self = this;

            wv.setWebViewClient(new WebViewClient() {
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    redirecting = true;
                    view.loadUrl(url);
                    return true;
                }
                public void onPageFinished(WebView view, String url) {
                    if(redirecting){
                        pDialog.dismiss();
                    }
                }
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    if(redirecting){
                        pDialog = new ProgressDialog(self);
                        pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                        pDialog.setMessage(" Loading...");
                        pDialog.setCancelable(true);
                        pDialog.show();
                    }

                }
            });


            pDialog = new ProgressDialog(this);
            pDialog.setMessage("Getting event...");
            pDialog.show();

            wv.setWebChromeClient(new WebChromeClient() {
                public void onProgressChanged(WebView view, int progress) {

                    if(progress == 100)
                        pDialog.dismiss();
                }
            });

            wv.loadUrl(url);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}

这是我的WebView的android类。我试图从这个网页中提取信息......

Api WebView

我只需要知道在WebView中单击了哪个按钮,并获得该按钮旁边的价格。然后我会将它发送给appsflyer进行跟踪。

2 个答案:

答案 0 :(得分:1)

你必须使用java脚本欺骗它,因为在加载页面后你必须将你的javascript注入加载的网页,如下所示

@Override
public void onPageFinished(WebView view, String url){
   // your javascript as string which works fine in scratchpad
   String javaScript ="javascript:(function() {alert();})()"; 
   webview.loadUrl(javaScript);
}

这样你就可以根据你的要求注入javascript并找到相应的DOM,并根据你可以调用this之类的java方法。

答案 1 :(得分:0)

你无法在webview中访问按钮点击事件,webview用于运行html页面,为此你必须在你的html页面内编码,使用JavaScript捕获onClick事件