2016-09-23 162 views
-5

我正在写一个android应用程序,需要从服务器获取数据。 如何在每次下载所有数据时知道数据是否发生变化?移动应用程序和服务器

你认为依靠日期和时间会工作吗?我的意思是: 如果服务器告诉应用程序更新的最后一次是在11.00 和当前时间是11.01,这意味着有更新{服务器还告诉我已经作出具体是什么更新 } 否则没有更新

回答

0

您可以使用一个待处理的意图以及报警管理器和广播接收器来实现此功能。

例如,你可以发送一个简单的请求,比如说“请求A”来获得服务器的响应。在服务器端,如果数据已更改,则响应将为“true”,否则为“false”。

现在,如果您将响应设为false,则无需下载完整的数据。如果回应是真的,应用程序应该开始下载数据,然后刷新内容。

你可以在说出“四小时”后设置你的等待意图。因此,在四个小时后,请求将被发送,您将收到回复。其次,您必须设置闹钟管理器并重复设置,以便每四小时发送一次请求。

您还需要一个广播接收器来接收广播。在BroadcastReceiver的onReceive中,您需要检查响应并根据它来刷新数据(如果它是真的)。

法火未决的意图每隔四个小时:

public void scheduleAlarmForDataDownload() { 
     Long time = new GregorianCalendar().getTimeInMillis()+1000 * 60 * 60 * 4;// current time + 4 Hrs 
     Intent intent = new Intent(this, AlarmReceiver.class); 
     PendingIntent intentAlarm = PendingIntent.getBroadcast(this, 0, intent, 0); 
     AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, 1000 * 60 * 60 * 4, intentAlarm);// 4 Hrs 
     //Toast.makeText(this, "Alarm Scheduled for 4 Hrs", Toast.LENGTH_LONG).show(); 
    } 

AlarmReceiver类

@Override 
    public void onReceive(Context context, Intent intent) { 
     // method to send a request(Request A) to server and check the response. 
     // If response is true again make a request to download and refresh the app data. 
     // If the response is false do nothing 

    } 
相关问题