Send Current Location to server periodically in android

后端 未结 5 1892
小鲜肉
小鲜肉 2020-12-14 22:32

I have to send my current location details (lat & long) to server periodically (Ex: for every 5 minutes). Is there any best way? I know how to get the current location &

5条回答
  •  再見小時候
    2020-12-14 22:55

    Register an alarm using AlarmManager to wake up after 5min when user open the application first time. create a service(fetch location and update to server) to run when alarm notifies your application. After the service finished the work , register for an alarm again to wake up after 5min. by this way you can achieve your task.

    ref

    Android: How to periodically send location to a server

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

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

    1st Edit - Adding code sample

    Step 1 - Create alarm manager and register alarm

    AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    
    Intent intent = new Intent(Main.this, YourWakefulReceiver.class);
    bool flag = (PendingIntent.getBroadcast(Main.this, 0,
                    intent, PendingIntent.FLAG_NO_CREATE)==null);
    /*Register alarm if not registered already*/
    if(flag){
    PendingIntent alarmIntent = PendingIntent.getBroadcast(Main.this, 0,
                        intent, PendingIntent.FLAG_UPDATE_CURRENT);
    
    // Create Calendar obj called calendar
            Calendar calendar = Calendar.getInstance();
    
    /* Setting alarm for every one hour from the current time.*/
    int intervalTimeMillis = 1000 * 60 * 60; // 1 hour 
    alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
                            calendar.getTimeInMillis(), intervalTimeMillis,
                            alarmIntent);
    }
    

    Step 2 - Create Receiver class

    public class YourWakefulReceiver extends WakefulBroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
                Intent service = new Intent(context, SimpleWakefulService.class);
                startWakefulService(context, service);
            }
        }
    }
    

    Setp 3 - Create Service class

    public class SimpleWakefulService extends IntentService {
    
        private static String tagName = "YourService";
    
        public SimpleWakefulService() {
            super("YourService");
        }
    
        @Override
        protected void onHandleIntent(Intent intent) {
            // Start your location
            LocationUtil.startLocationListener();
            try {
            // Wait for 10 seconds
                Thread.sleep(1000*10);
            } catch (InterruptedException e) {
            }
            //Stop location listener
            LocationUtil.stopLocationListener();
            // upload or save location
            uploadGps();
    
            SimpleWakefulReceiver.completeWakefulIntent(intent);
        }
    
    }
    

    Step 4 - Register service and receiver

    
            
    

    Note : This code is to understand the implementation. It will not compile.

提交回复
热议问题