Send Current Location to server periodically in android

后端 未结 5 1888
小鲜肉
小鲜肉 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 23:14

    I am developing an application that gets position of the cell phone all day long in 10 and 15 seconds in a service, it works fine but sometimes the method OnLocationChanged of the Network provider listener stop to being called.

     import android.location.Address;
        import android.location.Geocoder;
        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.view.View;
        import android.widget.AdapterView;
        import android.widget.ListView;
        import android.widget.SimpleCursorAdapter;
        import android.widget.TextView;
    
    
        import java.io.IOException;
        import java.util.List;
    
        import foodinn.databases.LocationUpdaterDatabase;
    
        public class LocationUpdatesActivity extends AppCompatActivity {
            private LocationUpdaterDatabase locationUpdaterDatabase;
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_location_updates);
    
                locationUpdaterDatabase = new LocationUpdaterDatabase(this, "Locations.db", null, 1);
    
                final Geocoder geocoder = new Geocoder(this);
    
                ListView listView = (ListView) findViewById(R.id.locationUpdatesListView);
    
                listView.setAdapter(new SimpleCursorAdapter(this, R.layout.location_updates_list_view_view, locationUpdaterDatabase.getLocationUpdates(), new String[] {"latitude", "longitude", "whenUpdated"}, new int[] {R.id.listViewTextView1, R.id.listViewTextView2, R.id.listViewTextView3}, SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER));
    
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView adapterView, View view, int i, long l) {
                        double latitude = Double.valueOf(((TextView) view.findViewById(R.id.listViewTextView1)).getText().toString());
                        double longitude = Double.valueOf(((TextView) view.findViewById(R.id.listViewTextView2)).getText().toString());
    
                        try {
                            List
    addressList = geocoder.getFromLocation(latitude, longitude, 1); String address = addressList.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex() String city = addressList.get(0).getLocality(); String state = addressList.get(0).getAdminArea(); String country = addressList.get(0).getCountryName(); String postalCode = addressList.get(0).getPostalCode(); String knownName = addressList.get(0).getFeatureName(); ((TextView) view.findViewById(R.id.listViewTextView4)).setText(address + ", " + city + ", " + country); } catch (IOException e) { e.printStackTrace(); } } }); } }

提交回复
热议问题