Note that there are some explanatory texts on larger screens.

plurals
  1. POLocationManager in onReceive() won't locate unless I call listener twice, but first call locates, and second call doesn't use new location
    primarykey
    data
    text
    <p>I admit I am relatively new to android, I come from python and C++. Help would be appreciated. I am simply calling a location update from Broadcast Receiver onReceive, so that I can text my device and receive it's location, updated at that moment.</p> <p>I am finding that I can call location be sending text, but it doesn't update the location, it just gives last known. If I call requestLocationUpdates TWICE, it appears to locate with the GPS icon on the device, but then the second call to requestLocationUpdates doesn't use the updated location, it uses the OLD location. If I DON'T call requestLocationUpdates TWICE, it doesn't even update (and no GPS icon blinking confirms this.) Here is my code, with the FIRST redundant call to requestLocationUpdates commented out.<br> I understand that the code isn't pretty (like the Thread.sleep's, which I intend to remove once the updates are working.. please forgive those temporary bits. Can somebody PLEASE help me, the code should logically be doing what I want, but logic doesn't seen to work with this one.. :</p> <p><code></p> <pre><code>package com.JT.appNew01; import java.util.ArrayList; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.preference.PreferenceManager; import android.telephony.SmsManager; import android.telephony.gsm.SmsMessage; import android.widget.Toast; import android.location.GpsStatus; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.location.Geocoder; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; public class SmsReceiverXxx extends BroadcastReceiver{ public static final String COMMAND = "SENDER"; public static final int SENDER_ACT_DOCUMENT = 0; public static final int SENDER_SRV_POSITIONING = 1; public static final int MIN_TIME_REQUEST = 5 * 1000; public static final String ACTION_REFRESH_SCHEDULE_ALARM = "com.JT.appNew01.ACTION_REFRESH_SCHEDULE_ALARM"; private static Location currentLocation;//was private static private static Location prevLocation;//was private static private static Context _context; private String provider = LocationManager.GPS_PROVIDER; private static Intent _intent; private static LocationManager locationManager;//was private static //I ADDED FOR FIRST PASS THE FOLLOWING: private LocationListener locList=new LocationListener(){ @Override public void onLocationChanged(Location location) { // TODO Auto-generated method stub try { gotLocation(location); } catch (Exception e) { } } @Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub try { String strStatus = ""; switch (status) { case GpsStatus.GPS_EVENT_FIRST_FIX: strStatus = "GPS_EVENT_FIRST_FIX"; break; case GpsStatus.GPS_EVENT_SATELLITE_STATUS: strStatus = "GPS_EVENT_SATELLITE_STATUS"; break; case GpsStatus.GPS_EVENT_STARTED: strStatus = "GPS_EVENT_STARTED"; break; case GpsStatus.GPS_EVENT_STOPPED: strStatus = "GPS_EVENT_STOPPED"; break; default: strStatus = String.valueOf(status); break; } } catch (Exception e) { e.printStackTrace(); } } };// I ADDED THIS TO GO WITH FIRST PASS!!!!! private static LocationListener locationListener = new LocationListener(){//was private static @Override public void onStatusChanged(String provider, int status, Bundle extras) { try { String strStatus = ""; switch (status) { case GpsStatus.GPS_EVENT_FIRST_FIX: strStatus = "GPS_EVENT_FIRST_FIX"; break; case GpsStatus.GPS_EVENT_SATELLITE_STATUS: strStatus = "GPS_EVENT_SATELLITE_STATUS"; break; case GpsStatus.GPS_EVENT_STARTED: strStatus = "GPS_EVENT_STARTED"; break; case GpsStatus.GPS_EVENT_STOPPED: strStatus = "GPS_EVENT_STOPPED"; break; default: strStatus = String.valueOf(status); break; } } catch (Exception e) { e.printStackTrace(); } } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } @Override public void onLocationChanged(Location location) { try { gotLocation(location); } catch (Exception e) { } } }; @Override public void onReceive(final Context context, Intent intent) { SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); String valuephn=sharedPrefs.getString("welcome_message", "NULL"); int zz = 0; int MAX_SMS_MESSAGE_LENGTH = 160; Bundle bundle = intent.getExtras(); SmsMessage[] msgs = null; String str = ""; Object[] pdus = (Object[]) bundle.get("pdus"); msgs = new SmsMessage[pdus.length]; if (bundle != null) { try { for (int i=0; i&lt;msgs.length; i++){ msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]); str += "Map Trax - Location: " + msgs[i].getOriginatingAddress(); str += " :"; str += msgs[i].getMessageBody().toString(); str += "\n"; zz=i; } } catch (Exception e){ e=e; } SmsManager sms = SmsManager.getDefault(); PendingIntent pendingIntent = null; //String xyzx = msgs[zz].getOriginatingAddress(); String xyzx="a"; // TEMPORARY FOR SKYPE!!! USE LINE ABOVE!!! // FIRST PASS TRY TO GET COORDS: //try{ // //if ( xyzx.contains(valuephn) ) { // if ( xyzx.contains("a") ) { // TEMPORARY FOR SKYPE!!! USE LINE ABOVE!!! // _context = context; // _intent = intent; // locationManager = (LocationManager) context // .getSystemService(Context.LOCATION_SERVICE); // if (locationManager.isProviderEnabled(provider)) { // locationManager.requestLocationUpdates(provider, MIN_TIME_REQUEST, // 5, locList); // // Location gotLoc = locationManager.getLastKnownLocation(provider); // gotLocation(gotLoc); // pauseOne(); // pauseTwo(); // pauseThree(); // pauseFour(); // pauseFive(); // pauseOne(); // pauseTwo(); // pauseThree(); // pauseFour(); // pauseFive(); // pauseOne(); // pauseTwo(); // pauseThree(); // pauseFour(); // pauseFive(); // OnNewLocationReceived(gotLoc);//JUST ADDED!!! // stopLocationListener();//JUST ADDED!!! // // // } else { // Location gotLoc2 = locationManager.getLastKnownLocation(provider); // pauseOne(); // pauseTwo(); // pauseThree(); // pauseFour(); // pauseFive(); // pauseOne(); // pauseTwo(); // pauseThree(); // pauseFour(); // pauseFive(); // pauseOne(); // pauseTwo(); // pauseThree(); // pauseFour(); // pauseFive(); // OnNewLocationReceived(gotLoc2);//JUST ADDED!!! // stopLocationListener();//JUST ADDED!!! // } // }}catch(Exception zzz){} // END FIRST PASS. try{ //if ( xyzx.contains(valuephn) ) { if ( xyzx.contains("a") ) { // TEMPORARY FOR SKYPE!!! USE LINE ABOVE!!! _context = context; _intent = intent; locationManager = (LocationManager) context .getSystemService(Context.LOCATION_SERVICE); if (locationManager.isProviderEnabled(provider)) { locationManager.requestLocationUpdates(provider, MIN_TIME_REQUEST, 5, locationListener); Location gotLoc = locationManager.getLastKnownLocation(provider); gotLocation(gotLoc); pauseOne(); pauseTwo(); pauseThree(); pauseFour(); pauseFive(); pauseOne(); pauseTwo(); pauseThree(); pauseFour(); pauseFive(); pauseOne(); pauseTwo(); pauseThree(); pauseFour(); pauseFive(); OnNewLocationReceived(gotLoc);//JUST ADDED!!! stopLocationListener();//JUST ADDED!!! } else { Location gotLoc2 = locationManager.getLastKnownLocation(provider); pauseOne(); pauseTwo(); pauseThree(); pauseFour(); pauseFive(); pauseOne(); pauseTwo(); pauseThree(); pauseFour(); pauseFive(); pauseOne(); pauseTwo(); pauseThree(); pauseFour(); pauseFive(); OnNewLocationReceived(gotLoc2);//JUST ADDED!!! stopLocationListener();//JUST ADDED!!! } //sms.sendTextMessage(valuephn,"", "Map Trax - GPS Co-ordinates:"+locationStringFromLocation(null).toString()+" - Map: http://maps.google.com/?q="+locationStringFromLocation(null).toString(),pendingIntent,pendingIntent); Toast.makeText(context, "1: "+locationStringFromLocation(null).toString(), Toast.LENGTH_SHORT).show(); locationManager.removeUpdates(locationListener); } }catch(Exception zzz){ try{ if ( xyzx.contains("a") ) { //sms.sendTextMessage(valuephn,"", "Map Trax - GPS Co-ordinates:"+locationStringFromLocation2(null).toString()+" - Map: http://maps.google.com/?q="+locationStringFromLocation2(null).toString(),pendingIntent,pendingIntent); Toast.makeText(context, "2: "+locationStringFromLocation2(null).toString(), Toast.LENGTH_SHORT).show(); locationManager.removeUpdates(locationListener); } }catch(Exception zzzz){ //sms.sendTextMessage(valuephn,"","Location currently unavailable",pendingIntent,pendingIntent); Toast.makeText(context, "Location currently unavailable", Toast.LENGTH_SHORT).show(); locationManager.removeUpdates(locationListener); } } }; } private void pauseFive() { // TODO Auto-generated method stub try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void pauseFour() { // TODO Auto-generated method stub try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void pauseThree() { // TODO Auto-generated method stub try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void pauseTwo() { // TODO Auto-generated method stub try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void pauseOne() { // TODO Auto-generated method stub try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static String locationStringFromLocation(final Location location) {//was public static return String.format("%.3f,%.3f", currentLocation.getLatitude(), currentLocation.getLongitude()); } public static String locationStringFromLocation2(final Location location) {//was public static return String.format("%.3f,%.3f", prevLocation.getLatitude(), prevLocation.getLongitude()); } private static void gotLocation(Location location) {//was public static prevLocation = currentLocation == null ? null : new Location(currentLocation); currentLocation = location; if (isLocationNew()) { OnNewLocationReceived(location); //Toast.makeText(_context, "new location saved", Toast.LENGTH_SHORT) // .show(); stopLocationListener(); } } private static boolean isLocationNew() {//was public static if (currentLocation == null) { return false; } else if (prevLocation == null) { return true; } else if (currentLocation.getTime() == prevLocation.getTime()) { return false; } else { return true; } } public static void stopLocationListener() {//was public static locationManager.removeUpdates(locationListener); } static ArrayList&lt;OnNewLocationListener&gt; arrOnNewLocationListener = new ArrayList&lt;OnNewLocationListener&gt;(); public static void setOnNewLocationListener( OnNewLocationListener listener) { arrOnNewLocationListener.add(listener); } public static void clearOnNewLocationListener( OnNewLocationListener listener) { arrOnNewLocationListener.remove(listener); } // This function is called after the new point received private static void OnNewLocationReceived(Location location) { // Check if the Listener was set, otherwise we'll get an Exception when // we try to call it if (arrOnNewLocationListener != null) { // Only trigger the event, when we have any listener for (int i = arrOnNewLocationListener.size() - 1; i &gt;= 0; i--) { arrOnNewLocationListener.get(i).onNewLocationReceived( location); } } } } </code></pre> <p></code></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload