如何在其他过程中使用AsyncTask?

时间:2019-01-01 11:25:22

标签: android android-asynctask

我正在使用android:process=":MyService"在应用程序的不同过程中使用服务,并且我认识到MyService类中的AsyncTasks无法正常运行,因此如何将数据与MyService类中的服务器同步?

我只想从此类更新Server,因为它是大多数情况下应该处于活动状态的ForegroundService。我相信将MyService类定义为一个单独的进程有助于我的应用程序的电池使用。我已经定义了一个内部类,并在MyService类中使用了它,但是它根本不起作用,我还尝试在MyService类中使用另一个自定义asyncTask类Object,但它也不起作用

这是我在MyService类中的代码的示例:

    static class UpdatePoint extends AsyncTask<String,String,String>{

    @Override
    protected String doInBackground(String... strings) {

        String userId = user.get(SessionManager.getKeyId()).toString();
        String coin = strings[1];
        String step = strings[2];

        String log_lat = strings[3];
        String log_lng = strings[4];
        String log_zone = strings[5];
        String log_address = strings[6];
        //Log.d("updatePoint","coin:"+coin+", step:"+step);
        try {
            String update_point_url = "http://mohregroup.ir/db-mapapa/updatePoint.php";
            URL url = new URL(update_point_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

            httpURLConnection.setConnectTimeout(5*1000);

            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
            String data = URLEncoder.encode("userId", "UTF-8") + "=" + URLEncoder.encode(userId, "UTF-8")
                    + "&" + URLEncoder.encode("coin", "UTF-8") + "=" + URLEncoder.encode(coin, "UTF-8")
                    + "&" + URLEncoder.encode("step", "UTF-8") + "=" + URLEncoder.encode(step, "UTF-8")
                    + "&" + URLEncoder.encode("log_lat", "UTF-8") + "=" + URLEncoder.encode(log_lat, "UTF-8")
                    + "&" + URLEncoder.encode("log_lng", "UTF-8") + "=" + URLEncoder.encode(log_lng, "UTF-8")
                    + "&" + URLEncoder.encode("log_zone", "UTF-8") + "=" + URLEncoder.encode(log_zone, "UTF-8")
                    + "&" + URLEncoder.encode("log_address", "UTF-8") + "=" + URLEncoder.encode(log_address, "UTF-8");
            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();

            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));

            StringBuilder response = new StringBuilder();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                response.append(line);
            }
            //Log.d("updatePoints response:", response.toString().trim());
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();

            Timber.d("done Successfully!");

            return response.toString();

        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

并在MyService类的另一个方法中使用它:

if (getAddress() != null) {
            if (getAddress().getFeatureName() == null && 
getAddress().getAddressLine(0) != null) {
                new UpdatePoint().execute(String.valueOf(coins), 
String.valueOf(steps),
                        String.valueOf(originLocation.getLatitude()), 
String.valueOf(originLocation.getLongitude()),
                        getAddress().getFeatureName(), "not titled");
            } else if (getAddress().getFeatureName() != null && 
getAddress().getAddressLine(0) == null) {
                new UpdatePoint().execute(String.valueOf(coins), 
String.valueOf(steps),
                        String.valueOf(originLocation.getLatitude()), 
String.valueOf(originLocation.getLongitude()),
                    "not titled", getAddress().getAddressLine(0));
        } else {
            new UpdatePoint().execute(String.valueOf(coins), String.valueOf(steps),
                    String.valueOf(originLocation.getLatitude()), String.valueOf(originLocation.getLongitude()),
                    getAddress().getFeatureName(), getAddress().getAddressLine(0));
        }
    } else {
        new UpdatePoint().execute(String.valueOf(coins), String.valueOf(steps),
                String.valueOf(originLocation.getLatitude()), String.valueOf(originLocation.getLongitude()),
                "not titled", "not titled");
    }

因此,关于

的任何想法

如何在具有不同进程的服务中使用asyncTask?

是否可以通过其他方法从具有不同过程的服务中更新服务器?

任何帮助将不胜感激!

谢谢。

1 个答案:

答案 0 :(得分:0)

一种方法可能是使用计时器,当计时器结束时,您可以调用asynctask并再次重置计时器。