【发布时间】:2019-01-01 11:25:21
【问题描述】:
我正在使用android:process=":MyService" 在我的应用程序的不同进程中使用服务,并且我认识到我的 MyService 类中的 AsyncTasks 无法工作和执行,那么如何将我的数据与 MyService 类中的服务器同步?
我只想从这个类更新我的服务器,因为它是一个 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?
或
还有其他方法可以从具有不同进程的服务更新服务器吗?
任何帮助将不胜感激!
谢谢。
【问题讨论】:
-
如果需要省电,你为什么不看看工作经理。然后您可以安排您的同步作业。您可以使用前台服务和普通处理程序线程或线程。我认为您不需要在不同的流程中提供服务
-
如果您觉得有用,请阅读并申请developer.android.com/training/sync-adapters/…
-
@Raghunandan 非常感谢,我会看看。
-
@ChandrakantDvivedi 非常感谢!我会尽快检查的。