Periodically fetching data (polling) from the server in Android

后端 未结 3 1244
北荒
北荒 2020-12-29 14:31

I working on the app where I get the data from the server using rest call and add it to the view. I get all the initial data correctly. I use AsyncTask for doing it.

3条回答
  •  自闭症患者
    2020-12-29 14:54

    You can checkout the AlarmManager class to do it.

    Intent intent = new Intent(this, MyAlarmManager.class);
    
    long scTime = 60*2000;//2mins
    
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
    
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + scTime, pendingIntent);
    

    here's the alarm Manager--

    public class MyAlarmManager extends BroadcastReceiver {
    
        Context _context;
            @Override
            public void onReceive(Context context, Intent intent) {
                _context= context;
                //connect to server..
    
            }
    }
    

    when ever the AlarmManager is 'fired' connect to the server again and populate the data you just recieved.

    http://developer.android.com/reference/android/app/AlarmManager.html

提交回复
热议问题