1

我有一个本机 android 应用程序进入 WebView 以进行结帐过程。我正在实施 appsflyer 来通过应用程序跟踪收入。如何检测页面上点击了哪个按钮,以及商品的收入价格是多少?

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 Web 视图

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

4

2 回答 2

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,并在此基础上调用 java方法

于 2017-08-18T01:13:01.023 回答
0

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

于 2017-08-18T00:55:00.943 回答