gpt4 book ai didi

android - 如何在不同的进程中使用 AsyncTask?

转载 作者:行者123 更新时间:2023-11-30 05:09:14 25 4
gpt4 key购买 nike

我正在使用 android:process=":MyService" 在我的应用程序的不同进程中使用一个服务,我认识到我在 MyService 类中的 AsyncTasks 不工作和执行,所以怎么能我在 MyService 类中将我的数据与我的服务器同步?

我只想从这个类更新我的服务器,因为它是一个应该在大多数时间处于 Activity 状态的 ForegroundService。我相信将 MyService 类定义为单独的进程有助于我的应用程序的电池使用。我已经定义了一个内部类并在 MyService 类中使用了它,但它根本不起作用,我还尝试在 MyService 类中使用另一个自定义 asyncTask 类对象,但它也不起作用

这是我在 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?

还有其他方法可以从具有不同进程的服务更新服务器吗?

任何帮助将不胜感激!

谢谢。

最佳答案

一种方法是使用计时器,当计时器完成后,您可以调用 asynctask 并再次重置计时器。

关于android - 如何在不同的进程中使用 AsyncTask?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53995038/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com