如何从Android发送到服务器的定期位置?我尝试过以下方法:
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 :(得分: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功能
`private void 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
}
}
//
}
`