Looper.prepare exception using LocationManager in external Service - android

I am getting the following exception when I try to use LocationManager within a custom class running in an external service:
*** Uncaught remote exception! (Exceptions are not yet supported across processes.)
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:200)
at android.os.Handler.<init>(Handler.java:114)
at android.location.LocationManager$GpsStatusListenerTransport$1.<init>(LocationManager.java:1464)
at android.location.LocationManager$GpsStatusListenerTransport.<init>(LocationManager.java:1464)
at android.location.LocationManager.addGpsStatusListener(LocationManager.java:1503)
at org.poseidon_project.contexts.hardware.GPSIndoorOutdoorContext.start(GPSIndoorOutdoorContext.java:97)
at org.poseidon_project.context.management.ContextManager.addObserverRequirement(ContextManager.java:97)
at org.poseidon_project.context.reasoner.ContextMapper.registerIndoorOutdoorsContext(ContextMapper.java:260)
at org.poseidon_project.context.reasoner.ContextMapper.registerContext(ContextMapper.java:197)
at org.poseidon_project.context.ContextReasonerCore.addContextRequirement(ContextReasonerCore.java:70)
at org.poseidon_project.context.ContextReasonerService$1.addContextRequirement(ContextReasonerService.java:74)
at org.poseidon_project.context.IContextReasoner$Stub.onTransact(IContextReasoner.java:74)
at android.os.Binder.execTransact(Binder.java:446)
Now, I have read many answer relating back to the use of Looper, with stuff like:
Looper.prepare;
mLocationManager.requestLocationUpdates(mProvider, mMinTime, mMinDistance, this, Looper.getMainLooper);
But this ends up not causing the Callback (onLocationChanged(Location location)) to be called when there is an update?
The class implements the LocationListener, which also invokes the LocatioManager methods:
public abstract class LocationContext extends ContextObserver implements LocationListener {
protected LocationManager mLocationManager;
private int mMinTime = 3000;
private int mMinDistance = 10;
private String mProvider = LocationManager.GPS_PROVIDER;
private String mIdealProvider = LocationManager.GPS_PROVIDER;
public LocationContext (Context c) {
super(c);
mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
}
public LocationContext (Context c, ContextReceiver cr) {
super(c, cr);
mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
}
public LocationContext (Context c, ContextReceiver cr, int minTime, int minDistance, String name) {
super(c, cr, name);
mMinTime = minTime;
mMinDistance = minDistance;
mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
}
public LocationContext (Context c, ContextReceiver cr, int minTime, int minDistance, String provider, String name) {
super(c, cr, name);
mMinTime = minTime;
mMinDistance = minDistance;
mProvider = provider;
mIdealProvider = provider;
mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
}
#Override
public boolean start() {
//new Thread(new Runnable() {
// #Override
// public void run() {
//Looper.prepare();
mLocationManager.requestLocationUpdates(mProvider, mMinTime, mMinDistance, this);
//handler.sendEmptyMessage(0);
//Looper.loop();
// }
//}).start();
mIsRunning = true;
//Looper.loop();
return true;
}
#Override
public boolean pause() {
return stop();
}
#Override
public boolean resume() {
return start();
}
#Override
public boolean stop() {
mLocationManager.removeUpdates(this);
mIsRunning = false;
return true;
}
#Override
public void onLocationChanged(Location location) {
checkContext(location);
}
protected abstract void checkContext(Location location);
#Override
public void onProviderDisabled(String provider) {
if (provider.equals(mIdealProvider)) {
mProvider = LocationManager.GPS_PROVIDER;
if (! mLocationManager.isProviderEnabled(mProvider)) {
Intent gpsOptionIntent = new Intent (android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(gpsOptionIntent);
}
}
}
#Override
public void onProviderEnabled(String provider) {
if ((provider.equals(mIdealProvider)) && (! provider.equals(mProvider))) {
mLocationManager.removeUpdates(this);
mProvider = provider;
mLocationManager.requestLocationUpdates(mProvider, mMinTime, mMinDistance, this);
}
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
public int getMinTime() {
return mMinTime;
}
public void setMinTime(int mMinTime) {
this.mMinTime = mMinTime;
}
public int getMinDistance() {
return mMinDistance;
}
public void setMinDistance(int mMinDistance) {
this.mMinDistance = mMinDistance;
}
I don't really understand how to use the Looper in my situation. Can someone help? I understand the answer "run in UI thread" but this is a single service app, there is no UI, so I don't think I can do it in the UI thread?
****UPDATE*****Solution Found******
I believe I found a solution. The class in question was an abstract class, which I was extending by a few classes that did various Location based operations.
In the LocationContext abstract class I used:
mLocationManager.requestLocationUpdates(mProvider, mMinTime, mMinDistance,this, Looper.getMainLooper());
And in an implementation class (for example one for analysing GPS satellite status) I placed it in a new Thread:
new Thread(new Runnable() {
#Override
public void run() {
Looper.prepare();
GPSIndoorOutdoorContext.super.start();
mLocationManager.addGpsStatusListener(gpsListener);
Looper.loop();
}
}).start();

mLocationManager.requestLocationUpdates(mProvider, mMinTime, mMinDistance, this);
is getting called from a NON UI Thread. Make sure you call your init or call your method in the UI Thread. You're probably initiating LocationContext or calling start method from a NON UI Thread, which you can't do. To request location updates, it must be called from the UI Thread.

Related

Wait for an async callback in a periodic task?

I'd like to check the user's location every 4 hours or so, and I don't want to leave my app running to do this. It looks like using a GcmTaskService with a PeriodicTask will let my service get called (WakefulBroadcastReceiver has restrictions against starting tasks when the app is stopped in Android 6+), and it will be compatible back to Android 4.4 (unlike JobScheduler) - my lowest supported Android version.
The issue is that GcmTaskService's onRunTask method is synchronous, but I want to use that to ask for a location and process the result (LocationManager will call my LocationListener implementation asynchronously).
How should this be handled?
Use a simple wait/notify:
private interface RunnableLocationListener extends Runnable, LocationListener {}
#Override
public int onRunTask (TaskParams params) {
final Object monitor = new Object();
final AtomicBoolean located = new AtomicBoolean();
new Thread(new RunnableLocationListener() {
Context context = PeriodicCollector.this;
public void run() {
Criteria criteria = new Criteria();
criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);
LocationManager locationManager = locationManager = (LocationManager)PeriodicCollector.this.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestSingleUpdate(criteria, this, Looper.getMainLooper());
}
#Override
public void onLocationChanged(Location location) {
// handle location here
synchronized (monitor) {
located.set(true);
monitor.notifyAll();
}
}
#Override public void onProviderDisabled(String s) {}
#Override public void onProviderEnabled(String s) {}
#Override public void onStatusChanged(String s, int i, Bundle b) {}
}).start();
int status = GcmNetworkManager.RESULT_FAILURE;
try {
synchronized (monitor) {
if (!located.get()) {
monitor.wait();
}
}
} catch (InterruptedException e) {
status = GcmNetworkManager.RESULT_SUCCESS;
}
return status;
}

running an script only for an specific time

I have a code for detecting location that I want to works only for 2 minutes.
when I fire start() method script must works almost for 2 minutes.
problem is in there that how run my script only for an specific time.
I used this code but don't running correct.
don't fire stop() method from in Timer().schedule()
public class a implements LocationListener{
private LocationManager locationManager;
private String provider;
private Location lastloc;
private Context _context;
public a(Context context){
_context = context;
}
public void start(){
locationManager = (LocationManager) _context.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, (LocationListener) this);
new Timer().schedule(
new TimerTask(){
public void run() {
stop();
}
}
,System.currentTimeMillis(), 2*60*1000);
}
public void stop(){
Log.d("states","stop");
locationManager.removeUpdates((LocationListener) this);
}
#Override
public void onLocationChanged(Location location) {
Log.d("states", "onLocationChanged()");
lastloc = location;
}
#Override
public void onProviderDisabled(String arg0) {
}
#Override
public void onProviderEnabled(String arg0) {
}
#Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
}
Check your code of Timer again. Usually, the Timer code should be like:
Timer.schedule(TimerTask,
delayTime); // delay a task before executed for the first time, in milliseconds
Because of your delayTime has been executed by using System.currentTimeMillis(), the System picks the current time in milliseconds since midnight, so that the TimerTask will be executed after millions millisecond.
Hence, use this code:
Timer timer = new Timer();
timer.schedule(new TimerTask(){
#Override
public void run() {
// do your thing here
}
}, 2*60*1000);
See this documentation about the type of Timer you created.
I finally solved my problem by using handlers.
read this page: Using Bundle Android to Exchange Data Between Threads
a.java
public class a implements LocationListener{
private LocationManager locationManager;
private String provider;
private Location lastloc;
private Context _context;
private Thread workingthread = null;
final Handler mHandler = new Handler(){
public void handleMessage(Message msg) {
Log.d("states","return msg from timer2min");
if(msg.what==1){
stop();
}
super.handleMessage(msg);
}
};
public a(Context context){
_context = context;
}
public void start(){
locationManager = (LocationManager) _context.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, (LocationListener) this);
workingthread=new timer2min(mHandler);
workingthread.start();
}
public void stop(){
Log.d("states","stop");
locationManager.removeUpdates((LocationListener) this);
}
#Override
public void onLocationChanged(Location location) {
Log.d("states", "onLocationChanged()");
}
#Override
public void onProviderDisabled(String arg0) {
}
#Override
public void onProviderEnabled(String arg0) {
}
#Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
}
timer2min.java
public class timer2min extends Thread {
private Handler hd;
public timer2min(Handler msgHandler){
hd = msgHandler;
}
public void run() {
try {
Thread.sleep(2*60*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Message msg = hd.obtainMessage();
msg.what = 1;
hd.sendMessage(msg);
}
}

Android LocationListener on Service with Thread

I´d like to implement a LocationListener in this way.
1 - Activity_first - Call LocationListener, but I have some spinner and this don´t respond when the LocationListener are onStart(). I´d like to implement with a Thread.
2 - When the onLocationChanged() in LocationListener gets called, I call Geocoder, and when the address is different than null, I save it on the shared preferences, and stop it.
Point 2, must be done in the background because the user might be in an Activity when I get the Geocoder's address (about 10 seconds, it depend on connection)
I was testing with IntentService, but of course, I doesn't work.
My Code
public class Carga_1 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.carga_1);
// Todo Create Spinner, etc
startService(new Intent(Carga_1.this, LocationService2.class));
}
}
public class LocationService2 extends Service {
public static final String BROADCAST_ACTION = "Hello World";
//public LocationManager locationManager; testted
//public MyLocationListener listener; tested
public Location previousBestLocation = null;
Thread my_thread;
#Override
public IBinder onBind(Intent intent) {
Log.d("[GPS_coord]", "????");
return null;
}
#Override
public void onCreate() {
super.onCreate();
}
#Override
public void onStart(Intent intent, int startId) {
Runnable r = new Runnable() {
public void run() {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
MyLocationListener listener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);
}
};
my_thread = new Thread(r);
my_thread.start();
}
#Override
public void onDestroy() {
super.onDestroy();
my_thread.stop();
}
private void get_address(double lat, double longi) {
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses;
try {
addresses = geocoder.getFromLocation(lat, longi, 1);
//SAVE ON SharedPrefereces
this.stopSelf();
}
catch (IOException e) {
Log.d("[GPS_geocoder]", "Decodificacion Erronea");
}
}
public class MyLocationListener implements LocationListener {
public void onLocationChanged(final Location loc) {
Log.i("GPS_Service", "Location changed");
Log.d("GPS_Service", String.valueOf(loc.getLatitude() + " : " + String.valueOf(loc.getLongitude())));
get_address(loc.getLatitude(), loc.getLongitude());
}
public void onProviderDisabled(String provider) {
Log.i("GPS_Service", "DES_habilitado");
}
public void onProviderEnabled(String provider) {
Log.i("GPS_Service", "Habilitado");
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}

Android Service Geolocation

I have one Android Service. This Service launch one class for geolocation. This is the code.
public class Localizar implements LocationListener {
private LocationManager manejador;
private Context context;
private DBAdapter db;
public Localizar(Context context) {
this.context = context;
db = new DBAdapter(context);
}
public void start() {
manejador = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (manejador.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
manejador.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 500, 0, this);
}else {
}
}
public void stop() {
manejador.removeUpdates(this);
}
private void saveData(double longitude, double latitude) {
Toast.makeText(context, "G R A B A N D O D A T O S", Toast.LENGTH_LONG).show();
}
public void onLocationChanged(Location location) {
saveData(location.getLatitude(), location.getLongitude());
}
public void onProviderDisabled(String proveedor) {
}
public void onProviderEnabled(String proveedor) {
}
public void onStatusChanged(String arg0, int arg1, Bundle bundle) {
}
}
My problem is that the method onLocationChanged never executed. What have I done wrong ?
Thank you!
I tried it on another phone and it works correctly, maybe my phone is broken. I have a samsung galaxy DUOS.....
There is a lot reasons why you don't get onLocationChanged
If you run it in Service try add Looper.getMainLooper(), like:
manejador.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
500,
0,
this,
Looper.getMainLooper()
);

Get Current Location From Android Background Service Issue

I want to develop location based reminder app. Therefore I want to use android service for get current location even app is not running. But I didn't do. I set timer in android service class but I don't know how to get current location in service class. What is the problem ? I got some error like this:
can't create handler inside thread that has not called looper.prepare()
public class TrackerService extends Service {
double latShared;
double lngShared;
double latService;
double lngService;
private LocationManager lm;
Timer timer;
Handler handler;
final static long TIME = 15000;
SharedPreferences mSharedPrefs;
SharedPreferences.Editor mPrefsEditor;
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
mSharedPrefs = getSharedPreferences("locationXML", MODE_PRIVATE);
latShared = (double)mSharedPrefs.getFloat("lat", 0);
lngShared = (double)mSharedPrefs.getFloat("lng", 0);
timer = new Timer();
timer.schedule(new TimerTask(){
#Override
public void run(){
LocationUpdates();
}
},0,TIME);
}
public void LocationUpdates(){
locListener locList = new locListener();
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locList);
}
#Override
public void onDestroy() {
//lm.removeUpdates(this);
timer.cancel();
}
public class locListener implements LocationListener{
#Override
public void onLocationChanged(Location location) {
latService = location.getLatitude();
lngService = location.getLongitude();
}
#Override
public void onProviderDisabled(String provider) {}
#Override
public void onProviderEnabled(String provider) {}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
}
LocationManager.requestLocationUpdates is supposed to run on UI thread as its sibling method defines. On the other hand TimerTask introduces a new thread to offload its execution.
However, if you are not using a new process for your service then simply call LocationUpdates() as below:
#Override
protected void onCreate() {
mSharedPrefs = getSharedPreferences("locationXML", MODE_PRIVATE);
latShared = (double)mSharedPrefs.getFloat("lat", 0);
lngShared = (double)mSharedPrefs.getFloat("lng", 0);
final Handler h = new Handler();
h.post(new Runnable() {
#Override
public void run() {
LocationUpdates();
h.postDelayed(this, TIME);
}
});
}
OR, if you don't want to use handler then simply upgrade your requestLocationUpdates as below:
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locList, getMainLooper());
In above case, getMainLooper() will dispatch your location updates from UI thread.

Categories

Resources