启用gps后,每30分钟发送一次位置更新

时间:2014-10-24 07:32:21

标签: android gps sms

嗨,我在这里取了我的代码。我想在gps启用时每隔30分钟通过短信发送位置更新。现在我的问题是这个代码顺序发送短信。所以,我想在这里使用线程。如何使用线程?任何人帮助我。

 protected LocationManager locationManager;
protected LocationListener locationListener;

@Override  
public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.autocapture);  
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

}  



@Override
public void onLocationChanged(Location location) {
    double lati = location.getLatitude();
    double longi = location.getLongitude();
    final String l_lati = String.valueOf(lati).trim();
    final String l_longi = String.valueOf(longi).trim();
    sendSMS(l_lati,l_longi);

}
@Override
public void onProviderDisabled(String provider) {

    Log.d("Latitude","disable");
}
@Override
public void onProviderEnabled(String provider) {

    Log.d("Latitude","enable"); 
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

    Log.d("Latitude","status");
}

2 个答案:

答案 0 :(得分:1)

实际上您需要将long minTime参数设置为30分钟,然后您将每30分钟获得一次更新。 这里:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

而不是0,你必须设置30 * 60 * 1000。

请查看文档:

http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(java.lang.String,long,float,android.location.LocationListener)

答案 1 :(得分:1)

用此编码替换你的onCreate块,

@Override  
    public void onCreate(Bundle savedInstanceState) 
   {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.autocapture);  

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30 * 60 * 1000, 0, this);

        sendSMS();
        finish();
    }