0

I am developing an application in which I am uploading the latitude and longitude to server after pressing a button on screen the a asynch task is starting and performing the operation and uploading the latitude and longitude to server port. Now the problem is that the gps could not find the latitude and longitude . It throws null on server port. Please help me my code is given below..... Thanks in advance

class Server extends AsyncTask<String, Integer, String> implements LocationListener{
@Override
protected String doInBackground(String... arg0) {

Looper.myLooper();
Looper.prepare();
for(int i=0;i<9898;i++){
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,this);
sendstring=phonenumber+latitude+longitude+"12345467788";
System.out.println(sendstring+"4532748");
stopUsingGPS();
try {
StrictMode.ThreadPolicy policy = new   StrictMode.ThreadPolicy.Builder().permitNetwork().build();
 StrictMode.setThreadPolicy(policy);
 Socket socket = null;
 try {
 int REDIRECTED_SERVERPORT=31997;
 InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
 socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
                     } catch (UnknownHostException e1) {
                        e1.printStackTrace();
                     } catch (IOException e1) {
                        e1.printStackTrace();
                     }                               
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);

out.println(sendstring); 
                     /*latitude=0.0;
                     longitude=0.0;*/
            }

                 catch (UnknownHostException e1) {
                    e1.printStackTrace();
                 } catch (IOException e1) {
                    e1.printStackTrace();
                 }




              catch (Exception e) {


              }
        }   
            return call1;
        }

        @Override
public void onLocationChanged(Location location) {


             latitude=location.getLatitude();
             longitude=location.getLongitude();
        }
@Override
public void onProviderDisabled(String provider) {


        }
@Override
public void onProviderEnabled(String provider) {
    }
@Override
public void onStatusChanged(String provider, int status,
                Bundle extras) {
}

public void stopUsingGPS(){
            if(locationManager != null){
                locationManager.removeUpdates(this);
            }       
        }   
  • Use this http://stackoverflow.com/questions/18051958/use-service-to-get-gps-location-android/18052280#18052280 – Mayank Saini Feb 07 '14 at 07:34
  • @Mayank Saini how to use without using Network provider? – appukrb Feb 07 '14 at 07:35
  • i have posted the answer without the network provider. Its just that it uses the GPS and in case GPS is unavailable it uses the network provider. I have already removed the network provider in the answer posted below. – Mayank Saini Feb 07 '14 at 07:40

1 Answers1

0

The best way would be to use location services by CWAC Location Poller service is already made for us to use it just you have to give the time interval to wake up it

Do it like dis way n you'll need the jar file which you can get it from [here]https://github.com/commonsguy/cwac-locpoll

From your activity start LocationPoller and set the alarm repeating to the time you want

AlarmManager alarmMgr = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent i = new Intent(this, LocationPoller.class);

Bundle bundle = new Bundle();
LocationPollerParameter parameter = new LocationPollerParameter(bundle);
parameter.setIntentToBroadcastOnCompletion(new Intent(this,
        LocationReceiver.class));
// try GPS and fall back to NETWORK_PROVIDER
parameter.setProviders(new String[] { LocationManager.GPS_PROVIDER });
parameter.setTimeout(120000);
i.putExtras(bundle);

PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
        SystemClock.elapsedRealtime(), 300000, pi);

Make a receiver class Location Receiver from where you'll fetch the lat n lon

public class LocationReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {


    try {      
      Bundle b=intent.getExtras();

      LocationPollerResult locationResult = new LocationPollerResult(b);

      Location loc=locationResult.getLocation();
      String msg;

      if (loc==null) {
        loc=locationResult.getLastKnownLocation();

        if (loc==null) {
          msg=locationResult.getError();
        }
        else {
          msg="TIMEOUT, lastKnown="+loc.toString();
        }
      }
      else {
        msg=loc.toString();



        Log.i("Location Latitude", String.valueOf(loc.getLatitude()));
        Log.i("Location Longitude", String.valueOf(loc.getLongitude()));
        Log.i("Location Accuracy", String.valueOf(loc.getAccuracy()));




      }

      Log.i(getClass().getSimpleName(), "received location: " + msg);   



    }
    catch (Exception e) {
      Log.e(getClass().getName(), e.getMessage());
    }
  }

and add this to your manifest

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

   <receiver android:name="com.commonsware.cwac.locpoll.LocationPoller" />

   <service android:name="com.commonsware.cwac.locpoll.LocationPollerService" />

and the receiver declaration where you'll fetch everything

<receiver android:name="com.RareMediaCompany.Helios.LocationReceiver" />
Mayank Saini
  • 3,017
  • 24
  • 25
  • LocationPollerParameter,LocationPollerResult showing error – user3110606 Feb 07 '14 at 08:48
  • Sorry that was an outdated library use this lib [https://www.dropbox.com/sh/pgxk2v9l5vl0h2j/3svyZnuwOK/CWAC-LocationPoller.jar – Mayank Saini Feb 07 '14 at 08:57
  • E/LocationPoller(4626): Invalid Intent -- has no provider showing this error – user3110606 Feb 07 '14 at 09:07
  • this does not return the lat long logcat is showing below 02-07 15:22:30.452: D/dalvikvm(8118): threadid=13: interp stack at 0x5e643000 02-07 15:22:50.435: D/dalvikvm(8118): create interp thread : stack size=32KB 02-07 15:22:50.438: D/dalvikvm(8118): create new thread 02-07 15:22:50.438: D/dalvikvm(8118): new thread created 02-07 15:22:50.439: D/dalvikvm(8118): update thread list 02-07 15:22:50.444: D/dalvikvm(8118): threadid=14: interp stack at 0x5e64b000 02-07 15:22:50.446: D/dalvikvm(8118): threadid=14: created from interp 02-07 15:22:50.446: D/dalvikvm(8118): start new thread – user3110606 Feb 07 '14 at 09:54
  • Can you tell me what is min time required to get latitude and longitude – user3110606 Feb 07 '14 at 09:56
  • Are these logs printing? Log.i("Location Latitude", String.valueOf(loc.getLatitude())); Log.i("Location Longitude", String.valueOf(loc.getLongitude())); Log.i("Location Accuracy", String.valueOf(loc.getAccuracy())); – Mayank Saini Feb 07 '14 at 10:12
  • no these are not printed I am calling alarm manager after 20 sec. – user3110606 Feb 07 '14 at 10:27
  • listen just have a breakpoint on 'onReceive' and try and debug it and see if location is coming – Mayank Saini Feb 07 '14 at 10:45