2016-03-03 62 views
0

如何從Android定期發送位置到服務器?我試過以下內容:從Android應用定位到PHP服務器的GPS定位

Intent i = new Intent(this, UpdateLocation.class); 
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT); 

// Repeat the notification every 15 seconds (15000) 
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);    


am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * Interval, pi); 

回答

0

創建服務將位置傳遞給服務器。

public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId);

if(i!=null) 
    { 
     timer = new Timer(); 
     TimerTask myTask = new TimerTask(){ 

      @Override 
      public void run() { 
       // TODO Auto-generated method stub 
       handler.post(new Runnable() { 
        public void run() { 
         //Perform GUI updation work here 
         //Toast work also 
         startTimer(); 
        } 
       }); 
      } 

     }; 
     timer.schedule(myTask,10000,60000); \\Time is 60000 milisecond 




    } 
    else 
    { 

     onDestroy(); 
    } 

     return START_REDELIVER_INTENT; 

} 

`

startTimer所功能

`私人無效startTimer所(){// TODO自動生成方法存根

// timer = new Timer(); 
// TimerTask myTask = new TimerTask(); 





      Log.e("LOCATION SERVICE->out","THREAD IS RUN"); 
      gps = new GpsTracker(SendLocationService.this); 
      lm = (LocationManager) getSystemService(LOCATION_SERVICE); 
      if(gps.canGetLocation()){ 
         double latitude = gps.getLatitude(); 
         double longitude = gps.getLongitude(); 
         Log.e("LOCATION",latitude+" "+longitude); 
         if(latitude!=0.0 && longitude!=0.0) 
         { 

         addressText = latitude+","+longitude; 

         // HERE YOU CAN PASS DATA TO SERVER 


         } 


        } 


// 
} 

`