Android 安卓:如何自动同步数据
在我的应用程序中,我将从服务器获取大量数据。我想通过自动与设备同步所有数据。即使我的应用程序未被使用,我也希望同步数据 现在,我已经使用AsyncTask完成了这项工作。但获取数据需要很长时间 这是我在MainActivity中编写的代码Android 安卓:如何自动同步数据,android,android-asynctask,android-syncadapter,android-async-http,Android,Android Asynctask,Android Syncadapter,Android Async Http,在我的应用程序中,我将从服务器获取大量数据。我想通过自动与设备同步所有数据。即使我的应用程序未被使用,我也希望同步数据 现在,我已经使用AsyncTask完成了这项工作。但获取数据需要很长时间 这是我在MainActivity中编写的代码 public class UpdateTask extends AsyncTask<Void, Void, Void> { private Context mCon; public UpdateTask(Context con
public class UpdateTask extends AsyncTask<Void, Void, Void> {
private Context mCon;
public UpdateTask(Context con)
{
mCon = con;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... nope) {
try {
// Set a time to simulate a long update process.
//Thread.sleep(4000);
getData();
getProjectInit();
getProjRequirements();
return null;
} catch (Exception e) {
return null;
}
}
@Override
protected void onPostExecute(Void nope) {
}
public void getProjRequirements()
{
projList = Project.listAll();
for(int i = 0;i<projList.size();i++)
{
proj = projList.get(i);
RequestParams params2 = new RequestParams();
AsyncClientHandler.get("projectMaster/Getdetails/"+proj.p_id, params2, new AsyncHttpResponseHandler()
{
@Override
public void onFailure(int statusCode, Header[] headers,
Throwable error, String content) {
// TODO Auto-generated method stub
super.onFailure(statusCode, headers, error, content);
}
@SuppressWarnings("deprecation")
@Override
public void onSuccess(int statusCode, Header[] headers,
String content) {
// TODO Auto-generated method stub
super.onSuccess(statusCode, headers, content);
//Log.i("projectid",proj.p_id.toString());
try
{
JSONObject data = new JSONObject(content);
Log.i("projreq_content",content.toString());
JSONArray projectrequirement = data.getJSONArray("requirements");
Log.i("requirement",projectrequirement.toString());
for(int i=0;i<projectrequirement.length();i++)
{
JSONObject projrequire = projectrequirement.getJSONObject(i);
Log.i("projreqwqwq",projrequire.toString());
prrq = ProjectRequirements.getProjectRequirementsByPidRid(getValue(projrequire,"id"),getValue(projrequire,"pid"),getValue(projrequire,"sid"));
if(prrq==null)
prrq=new ProjectRequirements();
prrq.pr_id=getValue(projrequire,"id");
prrq.pid=getValue(projrequire,"pid");
prrq.srid=getValue(projrequire,"srid");
prrq.sid=getValue(projrequire,"sid");
prrq.rid=getValue(projrequire,"rid");
prrq.estimated_value=getValue(projrequire,"estimated_value");
prrq.expected_close_date=getValue(projrequire, "expected_close_date");
prrq.others=getValue(projrequire,"others");
prrq.requirement_id=getValue(projrequire,"requirement_id");
prrq.name=getValue(projrequire,"name");
prrq.save();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
}
}
}
我已经给出了getProjectRequirements代码
但在其他片段中显示它需要很长时间
对于这种情况,我更喜欢什么?有人能给我举个例子或任何教程吗
我在android开发者网站上安装了场景同步适配器。但这对我来说很难
有人能给我举个简单的例子吗
提前谢谢 假设您定期同步,并且应用程序不需要 我会立即使用IntentService 为了让它定期运行,我会向AlarmManager注册它。如果这样做,即使应用程序未处于活动状态,它也将运行