如何在服务类中访问公共静态webview?

时间:2017-12-05 11:04:39

标签: android

我创建了MainActivity,其中我放了一个WebView,我希望在我的服务类中加载WebView个网址。我将WebView声明为public static,我的服务在调用onpause()onDestroy()方法时启动,但我的应用在调用onDestroy()方法时崩溃。

这是我的MainActivity代码:

public static WebView webView;
protected void onPause() {
    super.onPause();
    Log.d("CALL@@", "omPause");
    mIsInForegroundMode = false;
    if (call) {
        stopService(new Intent(MainActivity.this, AutoOpenAppService.class));
        stopStartService = "stopStartService";
        Intent intent = new Intent(this, AutoOpenAppService.class);
        intent.putExtra("stopStartService", stopStartService);
        startService(intent);
    } else {
        GETServiceStart = sharedPreferences.getBoolean("ServiceStart", ServiceStart);
        if (!GETServiceStart) {
            if (bv >= 21) {
                UnHideIconMethod();
            } else {
                UnHideIconMethod();
            }
        } else {
            Boolean isConnected = IsInternetConnect.isConnected();
            if (isConnected) {
                if (bv >= 21) {
                    HideIconMethod();
                } else {
                    HideIconMethod();
                }
                stopService(new Intent(MainActivity.this, AutoOpenAppService.class));
                stopStartService = "stopStartService";
                Intent intent = new Intent(this, AutoOpenAppService.class);
                intent.putExtra("stopStartService", stopStartService);
                startService(intent);
            } else {
                if (bv >= 21) {
                    HideIconMethod();
                } else {
                    HideIconMethod();
                }
                //  stopService(new Intent(MainActivity.this, AutoOpenAppService.class));
                //  startService(new Intent(this, AutoOpenAppService.class));
            }
        }
    }
}


protected void onDestroy() {
    super.onDestroy();
    Log.d("CALL@@", "omDestroy");
    mIsInForegroundMode = false;
    if (call) {
        stopStartService = "stopStartService";
        Intent intent = new Intent(this, AutoOpenAppService.class);
        intent.putExtra("stopStartService", stopStartService);
        //  stopService(new Intent(MainActivity.this, AutoOpenAppService.class));
        startService(intent);
    } else {
        GETServiceStart = sharedPreferences.getBoolean("ServiceStart", ServiceStart);
        if (!GETServiceStart) {
            if (bv >= 21) {
                UnHideIconMethod();
            } else {
                UnHideIconMethod();
            }
        } else {
            Boolean isConnected = IsInternetConnect.isConnected();
            if (isConnected) {
                if (bv >= 21) {
                    HideIconMethod();
                } else {
                    HideIconMethod();
                }
                stopStartService = "stopStartService";
                Intent intent = new Intent(this, AutoOpenAppService.class);
                intent.putExtra("stopStartService", stopStartService);
                stopService(new Intent(MainActivity.this, AutoOpenAppService.class));
                startService(intent);
            } else {
                if (bv >= 21) {
                    HideIconMethod();
                } else {
                    HideIconMethod();
                }
                // stopService(new Intent(MainActivity.this, AutoOpenAppService.class));
                startService(new Intent(this, AutoOpenAppService.class));
            }
        }
    }
}

这是我的服务类:

    private void callApi(final String data, final String mainUrl) {
    modelArrayList = new ArrayList<>();


    Log.d("MAINURL@@", "" + mainUrl);
    StringRequest stringRequest = new StringRequest(Request.Method.GET, mainUrl,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject obj = new JSONObject(response);
                        mainWVUrl = obj.getString("url");
                        Log.d("WVVisible@@", "" + mainWVUrl);
                        if (mainWVUrl != null) {
                            webView.loadUrl(mainWVUrl);
                        }
                        JSONArray jsonArray = obj.getJSONArray("timeRange");
                        for (int i = 0; i < jsonArray.length(); i++) {
                            minTime = jsonArray.getInt(0);
                            maxTime = jsonArray.getInt(1);
                        }
                        Log.d("LENGTH@@", "" + minTime + ":" + maxTime);
                        Boolean WVVisible = obj.getBoolean("visible");
                        if (WVVisible) {
                            webView.setVisibility(View.VISIBLE);
                        } else {
                            webView.setVisibility(View.INVISIBLE);
                        }

                        TimeOut = TimeUnit.SECONDS.toMillis(obj.getInt("timeout"));
                        Log.d("WVVisible@@", "" + WVVisible + ":" + TimeOut);

                        JSONArray UrlArray = obj.getJSONArray("urls");
                        Log.d("WVVisible@@", "" + WVVisible + ":" + TimeOut + ":" + UrlArray);
                        for (int j = 0; j < UrlArray.length(); j++) {
                            JSONObject jsonObject = UrlArray.getJSONObject(j);
                            String subUrl = jsonObject.getString("url");
                            long time = jsonObject.getInt("time");

                            UrlTimeModel urlTimeModel = new UrlTimeModel();
                            urlTimeModel.setSubUrl(subUrl);
                            urlTimeModel.setTime(time);
                            modelArrayList.add(urlTimeModel);

                            Log.d("WVVisible@@", "" + subUrl + ":" + time + modelArrayList);
                        }

                        if (data.equalsIgnoreCase("stopStartService")) {
                            Log.d("HELLO", "INNER");
                            ReOpenService(minTime, maxTime);
                        } else {
                            Log.d("HELLO", "OUTER");
                            if (modelArrayList.size() < 0) {
                                Handler handler = new Handler();
                                handler.postDelayed(new Runnable() {
                                    public void run() {
                                        webView.setVisibility(View.INVISIBLE);
                                    }
                                }, TimeOut);
                            } else {
                                LoadSubURl(modelArrayList, TimeOut);
                            }
                        }
                    } catch (JSONException e) {
                        Log.d("ARRAY@@", "" + e.toString());
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d("ARRAY@@@", "" + error.toString());
                  /*Random rn = new Random();
                    int range = 360 - 180 + 1;
                    int randomNum = rn.nextInt(range) + 180;
                    notify = (int) TimeUnit.SECONDS.toMillis((int) randomNum);
                    Log.d("MINIMUM123@@", "" + randomNum + ":" + notify);
                    startTimer();
                    Log.d("ARRAY@@@", "" + error.toString());*/
                }
            });
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

0 个答案:

没有答案