I have created an IntentService class which i used to send device data to a web service whether the app is running or is terminated. Here's my code:
import android.app.IntentService;
import android.content.Intent;
import android.location.Location;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
public class PostMyCoordinatesService extends IntentService implements GPSTracker.GPSUpdateListener,WebService.WebServiceListener {
GPSTracker gpsTracker;
DataCacher dataCacher;
WebService webService;
private String TAG = Constants.GLOBAL_TAG+"-PostService";
public static final String SERVICE_NOTIFIER ="<mypackagename>";
public PostMyCoordinatesService(String name) {
super(name);
}
public PostMyCoordinatesService(){
super("PostMyCoordinatesService.");
}
#Override
protected void onHandleIntent(Intent intent) {
Log.v(TAG,"HANDLE INTENT IS CALLED!");
startMyLocationUpdates();
}
private void startMyLocationUpdates() {
//reset location update callbacks if it exists.
if (gpsTracker != null) {
gpsTracker.stopUsingGPS();
}
gpsTracker = null;
dataCacher = DataCacher.getInstance();
if(dataCacher== null)
dataCacher = new DataCacher(getApplicationContext());
String seekbarCachedValue = dataCacher.getStringForKey(Constants.KEY_FREQUENCYUPDATES);
boolean allowGPS = dataCacher.getBooleanForKey(Constants.KEY_ALLOW_DEVICEGPS, false);
if(allowGPS) {
gpsTracker = new GPSTracker(getApplicationContext(), (Integer.parseInt(seekbarCachedValue) + 60));
gpsTracker.setOnGPSUpdateListener(this);
Log.v(TAG,"Starting gps updates.");
}
else{
Log.v(TAG, "Sending gps data was disabled.");
}
}
#Override
public boolean stopService(Intent name) {
super.stopService(name);
if (gpsTracker != null) {
gpsTracker.stopUsingGPS();
Log.v(TAG,"GPS tracking was disabled");
}
return true;
}
#Override
public void onGPSUpdated(Location updatedLocation) {
dataCacher = DataCacher.getInstance();
if (dataCacher == null)
dataCacher = new DataCacher(getApplicationContext());
boolean allowGPS = dataCacher.getBooleanForKey(Constants.KEY_ALLOW_DEVICEGPS, false);
if (allowGPS) {
Log.v(TAG, "Updating my coordinates>.." + updatedLocation.toString());
sendMyLocation(updatedLocation);
}
}
private void sendMyLocation(Location location){
dataCacher = DataCacher.getInstance();
if(dataCacher==null)
dataCacher = new DataCacher(getApplicationContext());
//get phone number
String phoneNumber = dataCacher.getUserPhone();
String url =Constants.URL_POST_DEVICE_LOCATION+
phoneNumber+"/"+
location.getLatitude()+"/"+
location.getLongitude()+"/"+
location.getSpeed()+"/"+
location.getBearing()+"/"+
location.getAltitude();
Log.v(TAG,">>update my coordinates url: "+url);
webService = new WebService(url,this);
webService.execute();
}
#Override
public void didReceiveData(String data, WebService caller) {
Log.v(TAG, "Coordinate Update Service: " + data);
LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(SERVICE_NOTIFIER));
}
#Override
public void didReceiveError(String errorMessage) {
Log.v(TAG,"Coordinate Update Service ERROR: "+errorMessage);
}
}
my logcat shows that my handle intent has been called:
08-27 14:03:01.989 26460-26534/? V/Test-PostService﹕ HANDLE INTENT IS CALLED!
08-27 14:03:02.003 26460-26534/? V/Test-PostService﹕ Starting gps updates.
But the callback method onGPSUpdated(Location updatedLocation) wasn't called. I am certain of my GPSTracker class' callbacks for I have used it on my previous projects. What could be the cause of this? What is the more efficient way to send updates to web service for both my app's running and terminated status. I am new to Service by the way. Any help would be greatly appreciated.
EDIT:
Here's my GPSTracker class.
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
public class GPSTracker extends Service implements LocationListener{
public interface GPSUpdateListener {
void onGPSUpdated(Location updatedLocation);
}
String TAG = Constants.GLOBAL_TAG+"-GPSTracker";
private final Context mContext;
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
Location location; // location
double latitude; // latitude
double longitude; // longitude
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1; //meters
// The minimum time between updates in milliseconds
private static long MIN_TIME_BW_UPDATES = 1000*60; //default 60 secs
// Declaring a Location Manager
protected LocationManager locationManager;
//snarf added variables
private static GPSTracker trackerInstance;
//snarf added methods
private GPSUpdateListener listener;
public void setOnGPSUpdateListener(GPSUpdateListener updateListener){
listener = updateListener;
}
public static GPSTracker getTrackerInstance(){
return trackerInstance;
}
public GPSTracker(Context context, int updateIntervalSeconds) {
this.mContext = context;
MIN_TIME_BW_UPDATES = updateIntervalSeconds * 1000;
Log.v(TAG,"starting location updates every "+MIN_TIME_BW_UPDATES+ " milliseconds.");
getLocation();
trackerInstance =this;
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
}
return location;
}
#Override
public void onLocationChanged(Location location) {
this.location = location;
getLatitude();
getLongitude();
if(listener!=null)
listener.onGPSUpdated(location);
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public IBinder onBind(Intent arg0) {
return null;
}
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}
return latitude;
}
public double getLongitude(){
if(location != null){
longitude = location.getLongitude();
}
return longitude;
}
public boolean canGetLocation() {
return this.canGetLocation;
}
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
alertDialog.setTitle("GPS is settings");
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
public void stopUsingGPS(){
if(locationManager != null){
locationManager.removeUpdates(GPSTracker.this);
Log.v(TAG,"Stopped gps updates!");
}
}
}
Related
I am using location service to listen location on background. But I have a problem with getting current location in service. It does not give me the proper result but 0,0 as coordinates. When I do the same operations on main activity, it gives the expected result. I think it is about context given to the gpstracker as a parameter but do not know what to do.
public class LocationService extends Service {
Context context;
Timer timer;
String latitude, longitude;
private String id;
GPSTracker gps;
public LocationService() {
}
#Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
#Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
id = intent.getStringExtra("id");
timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
int isHere = isHere();
Log.i("isHere", isHere + "");
updateUserStatus(isHere);
}
}, 0, 200000);
return START_STICKY;
}
private int isHere() {
gps = new GPSTracker(context);
if (!gps.canGetLocation()) {
gps.showSettingsAlert();
}
if (gps.canGetLocation()) {
latitude = gps.getLatitude() + "";
longitude = gps.getLongitude() + "";
if (Math.abs(39.875303 - Double.parseDouble(latitude)) <= 0.001 || Math.abs(32.879980 - Double.parseDouble(longitude)) <= 0.001)
return 1;
else
return 0;
}
return 0;
}
private void updateUserStatus(int isHere) {
Call<Kisi> x = ManagerAll.getInstance().updateLocation(id, isHere);
x.enqueue(new Callback<Kisi>() {
#Override
public void onResponse(Call<Kisi> call, Response<Kisi> response) {
if (response.isSuccessful())
Log.i("durum", "başarılı");
}
#Override
public void onFailure(Call<Kisi> call, Throwable t) {
}
});
}
}
public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
// flag for GPS status
boolean canGetLocation = false;
Location location; // location
double latitude; // latitude
double longitude; // longitude
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
// Declaring a Location Manager
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
/**
* Stop using GPS listener
* Calling this function will stop using GPS in your app
* */
public void stopUsingGPS(){
if(locationManager != null){
locationManager.removeUpdates(GPSTracker.this);
}
}
/**
* Function to get latitude
* */
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
/**
* Function to get longitude
* */
public double getLongitude(){
if(location != null){
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
/**
* Function to check GPS/wifi enabled
* #return boolean
* */
public boolean canGetLocation() {
return this.canGetLocation;
}
/**
* Function to show settings alert dialog
* On pressing Settings button will lauch Settings Options
* */
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("GPS is settings");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
#Override
public void onLocationChanged(Location location) {
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public IBinder onBind(Intent arg0) {
return null;
}
}
Taken here
try with this code.
this code work for me.
package com.yourpackage.tracker;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import com.orhanobut.hawk.Hawk;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import id.glite.transporter.GLiteApplication;
import id.glite.transporter.data.DataManager;
import id.glite.transporter.data.model.post.LocationUpdate;
import id.glite.transporter.util.rx.scheduler.SchedulerUtils;
import io.nlopez.smartlocation.SmartLocation;
import timber.log.Timber;
public class LocationTrackerService extends Service {
// constant
public static final long NOTIFY_INTERVAL = 3 * 60 * 1000; // 3 minutes
// run on another Thread to avoid crash
private Handler mHandler = new Handler();
// timer handling
private Timer mTimer = null;
private DataManager dataManager;
private Context mContext;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
mContext = this;
dataManager = GLiteApplication.get(getBaseContext()).getComponent().dataManager();
// cancel if already existed
if (mTimer != null) {
mTimer.cancel();
} else {
// recreate new
mTimer = new Timer();
}
// schedule task
mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, NOTIFY_INTERVAL);
}
class TimeDisplayTimerTask extends TimerTask {
#Override
public void run() {
// run on another thread
// display toast
mHandler.post(this::doTrackLocation);
}
private void doTrackLocation() {
if (idTask != null) {
SmartLocation.with(getBaseContext()).location()
.start(location -> {
List<Double> position = new ArrayList<>();
position.add(0, location.getLatitude());
position.add(1, location.getLongitude());
});
}
}
}
}
Make sure you enable the GPS on your phone and allowing the location permission as well.!
I want to make a notification that contain longitude and latitude in every 5 minutes. And I make 2 service, but I the longitude and latitude can not be displayed in my apps.
this is my GPS Service
package com.example.michael.eksperimen4gpstrack;
/**
* Created by Michael on 01/12/2015.
*/
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
import java.util.Timer;
public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
private static final long NOTIFY_INTERVAL = 10 * 1000; // 10 detik
private Timer mTimer = null;
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
// flag for GPS status
boolean canGetLocation = false;
Location location; // location
double latitude; // latitude
double longitude; // longitude
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
// Declaring a Location Manager
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
/**
* Stop using GPS listener
* Calling this function will stop using GPS in your app
* */
public void stopUsingGPS(){
if(locationManager != null){
locationManager.removeUpdates(GPSTracker.this);
}
}
/**
* Function to get latitude
* */
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
/**
* Function to get longitude
* */
public double getLongitude(){
if(location != null){
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
/**
* Function to check GPS/wifi enabled
* #return boolean
* */
public boolean canGetLocation() {
return this.canGetLocation;
}
/**
* Function to show settings alert dialog
* On pressing Settings button will lauch Settings Options
* */
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("GPS is settings");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
#Override
public void onLocationChanged(Location location) {
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public IBinder onBind(Intent arg0) {
return null;
}
}
And this is my timer service
package com.example.michael.eksperimen4gpstrack;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Handler;
/**
* Created by Michael on 14/12/2015.
*/
public class LocService extends Service
{
public static final long NOTIFY_INTERVAL = 5*1000; // 10 detik
private android.os.Handler mHandler = new android.os.Handler();
public Timer mTimer = null;
GPSTracker gps;
public IBinder onBind(Intent arg0)
{
return null;
}
public void onCreate()
{
if(mTimer != null)
{
mTimer.cancel();
}
else
{
mTimer = new Timer();
}
mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, NOTIFY_INTERVAL);
}
class TimeDisplayTimerTask extends TimerTask
{
#Override
public void run()
{
gps = new GPSTracker(getApplicationContext());
mHandler.post(new Runnable()
{
#Override
public void run()
{
// check if GPS enabled
if(gps.canGetLocation())
{
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
// \n is for new line
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude + "\n\n"+getDateTime(), Toast.LENGTH_LONG).show();
}
else
{
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}
}
});
}
}
public int onStartCommand(Intent intent, int flags, int startId)
{
Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
return START_STICKY;
}
public void onDestroy()
{
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show();
}
private String getDateTime()
{
// get date time in custom format
SimpleDateFormat sdf = new SimpleDateFormat("[yyyy/MM/dd - HH:mm:ss]");
return sdf.format(new Date());
}
}
When toast displayed, the longitude and latitude still 0.0, anyone can help me please?
Gratia
Replace your TimeDisplayTimerTask with following code:
Initialize gps within run method.
class TimeDisplayTimerTask extends TimerTask
{
#Override
public void run()
{
mHandler.post(new Runnable()
{
#Override
public void run()
{
gps = new GPSTracker(getApplicationContext());
// check if GPS enabled
if(gps.canGetLocation())
{
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
// \n is for new line
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude + "\n\n"+getDateTime(), Toast.LENGTH_LONG).show();
}
else
{
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}
}
});
}
}
Wait sometime because getting gps-coordinates from gps takes some time.
My problem is not new, but none of answers in Stackoverflow was helpful for me.
Situation: I have a service which works with an Alarm. it repeats every 1 minutes and run a service for me. in that service I need to send GPS data to a web server.
the codes are as below:
import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.os.AsyncTask;
import android.os.IBinder;
import bliksund.taxitracking.tools.Cachepref;
import bliksund.taxitracking.tools.PostData;
public class SendData extends Service {
Cachepref cachepref;
GET_GPS gps;
#Override
public void onCreate() {
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
cachepref = new Cachepref(this);
gps = new GET_GPS(this);
new getloc().execute();
return super.onStartCommand(intent, flags, startId);
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
private class getloc extends AsyncTask<Void, Void, Void> {
String username;
Double lt;
Double lg;
Location location;
#Override
protected Void doInBackground(Void... params) {
username = cachepref.getUsername();
location = gps.getLocation();
lt = location.getLatitude();
lg = location.getLongitude();
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
PostData postData = new PostData(username, 1, lt, lg);
super.onPostExecute(aVoid);
}
}
}
this is my service.
+ PostData will Post the parameters to a web page.
+ Cachepref saves username for user Session.
Problem: When the codes go to get GPS Location. it will make an error that says:
location = gps.getLocation(); // will cause to below line error
Can't create handler inside thread that has not called Looper.prepare()
I used AsynTask to solve this problem, but it is still there.
GET_GPS code are as below:
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
public class GET_GPS extends Service implements LocationListener {
private final Context mContext;
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
// flag for GPS status
boolean canGetLocation = false;
Location location; // location
double latitude; // latitude
double longitude; // longitude
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
protected LocationManager locationManager;
public GET_GPS(Context mContext) {
this.mContext = mContext;
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
public void stopUsingGPS(){
if(locationManager != null){
locationManager.removeUpdates(GET_GPS.this);
}
}
/**
* Function to get latitude
* */
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
/**
* Function to get longitude
* */
public double getLongitude(){
if(location != null){
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
/**
* Function to check GPS/wifi enabled
* #return boolean
* */
public boolean canGetLocation() {
return this.canGetLocation;
}
#Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
#Override
public void onLocationChanged(Location location) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
}
with the help of ρяσѕρєя K . he said to move these codes to PostExecute and it was running, the problem with PostData happened. and I I solved in the way below. I hope it helps others.
private class getloc extends AsyncTask<Void, Void, Void> {
String username;
Double lt;
Double lg;
Location location;
#Override
protected Void doInBackground(Void... params) {
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
username = cachepref.getUsername();
location = gps.getLocation();
lt = location.getLatitude();
lg = location.getLongitude();
new Thread(new Runnable() {
#Override
public void run() {
PostData postData = new PostData(username, 1, lt, lg);
}
}).start();
super.onPostExecute(aVoid);
}
}
I would like to run a locationlistener on my app, that send after every 10m or every 5 seconds an new "Popup". Later I will send my data to the cloud.
This is my GPSTracker class:
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;
public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
// flag for GPS status
boolean canGetLocation = false;
Location location; // location
double latitude; // latitude
double longitude; // longitude
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 5 * 1; // 1 minute
// Declaring a Location Manager
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
/**
* Stop using GPS listener
* Calling this function will stop using GPS in your app
* */
public void stopUsingGPS(){
if(locationManager != null){
locationManager.removeUpdates(GPSTracker.this);
}
}
/**
* Function to get latitude
* */
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
/**
* Function to get longitude
* */
public double getLongitude(){
if(location != null){
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
/**
* Function to check GPS/wifi enabled
* #return boolean
* */
public boolean canGetLocation() {
return this.canGetLocation;
}
/**
* Function to show settings alert dialog
* On pressing Settings button will lauch Settings Options
* */
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("GPS is settings");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
#Override
public void onLocationChanged(Location location) {
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
Toast.makeText(GPSTracker.this,"Provider enabled by the user. GPS turned on",Toast.LENGTH_LONG).show();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public IBinder onBind(Intent arg0) {
return null;
}
}
For my activityclass I have made this:
gps = new GPSTracker(MainActivity.this);
double latitude2 = gps.getLatitude();
double longitude2 = gps.getLongitude();
I receive the data, this works. But now I want to run the data in the background so when the location is changed I can implement a function that sends my data to the cloud.
I tried this:
private void startGPSTrackerInBackground() {
new AsyncTask<Void,Void,String>() {
#Override
protected String doInBackground(Void... params) {
String msg = "This message runs in background";
gps = new GPSTracker(MainActivity.this);
if(!gps.canGetLocation()){
gps.showSettingsAlert();
}else{
gps = new GPSTracker(MainActivity.this);
double latitude2 = gps.getLatitude();
double longitude2 = gps.getLongitude();
}
return msg;
}
#Override
protected void onPostExecute(String msg) {
mDisplay.append(msg + "\n");
}
}.execute(null, null, null);
}
But in this function it's not possible to get the information of my GPSTracker class, anyone an idea to make this possible?
Create an independent class, add a GPSTracker member variable and pass it in the constructor. Wrap your task in a method you can invoke later:
public class BackgroundGPSTracker() {
GPSTracker tracker;
public BackgroundGPSTracker(GPSTracker tracker) {
this.tracker = tracker;
}
public void run() {
new AsyncTask<Void,Void,String>() {
#Override
protected String doInBackground(Void... params) {
// Do some background stuff.
}
#Override
protected void onPostExecute(String msg) {
// Do after work stuff
}
}.execute(null, null, null);
}
}
Use:
If you create GPSTracker like this:
gps = new GPSTracker(MainActivity.this);
Then you start the activity passing it to the task:
BackgroundGPSTracker bgGPSTracker= new BackgroundGPSTracker(gps);
bgGPSTracker.run();
EDIT
Use a member variable to store last known location, and timer to schedule updates:
public class CampaignsDiscoverActivity extends Activity{
static final int QUERY_CAMPAINGS_DELAY = 30000;// milliseconds
Location currentLocation;
Timer timer;
void restartTimer() {
timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
sendDataToServerOrWhatever();
}
}, QUERY_CAMPAINGS_DELAY, QUERY_CAMPAINGS_DELAY);
}
void stopTimer() {
if (timer != null) {
timer.cancel();
timer = null;
}
}
void sendDataToServerOrWhatever() {
// Do some stuff using currentLocation
}
Set a listener for locations changes. when location changed is raised, stop the timer, do work and restart it:
void startGPSUpdates() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onLocationChanged(Location location) {
stopTimer();
currentLocation = location;
sendDataToServerOrWhatever();
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
// Initialize location.
currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (currentLocation == null) {
currentLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
restartTimer();
}
To start the system just invoke startGPSUpdates.
You should pause listeners when app goes to the background and resume when foreground:
#Override
public void onPause() {
super.onPause();
if (applicationData.isLoggedIn()) {
pauseLocationUpdates();
}
}
#Override
public void onResume() {
super.onResume();
if (applicationData.isLoggedIn()) {
resumeLocationUpdates();
}
}
void pauseLocationUpdates() {
locationManager.removeUpdates(locationListener);
}
void resumeLocationUpdates() {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
I think you should do the same with the timer, but the app where I'm using it is not finished and fully tested yet, so you might find bugs.
Hope that helps.
i've created an android application which there are two buttons start stop. when start button is clicked then on every 1 seconds current latitude and longitude values are send to the usl as post to insert into a remote mysl database.
within start button i wrote a service MyService for sending latitude and longitude.
within the onStartCommand(..) method of MyService i have wrote a timer for sending latitude and longitude at 1 second.
But i'm getting the following exception
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
Can anyone please tell me some solution for this
My Code is as given below
iLoadPage.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class iLoadPage extends Activity {
Button start,stop;
boolean flag=true;
double latin,longin,longitude,latitude;
Activity activity;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.iloadpage);
start = (Button) findViewById(R.id.startapp);
stop = (Button) findViewById(R.id.stop);
stop.setEnabled(false);
start.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
start.setEnabled(false);
stop.setEnabled(true);
String trackid= getIntent().getExtras().getString("trackid");
MyService.setTrackid(trackid);
startService(new Intent(getBaseContext(),MyService.class));
}
});
stop.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
start.setEnabled(true);
stop.setEnabled(false);
stopService(new Intent(getBaseContext(),MyService.class));
}
});
}
#Override
public void onDestroy() {
super.onDestroy();
}
public double getLatin() {
return latin;
}
public void setLatin(double latin) {
this.latin = latin;
}
public double getLongin() {
return longin;
}
public void setLongin(double longin) {
this.longin = longin;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
}
MyService .java
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ScheduledExecutorService;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.IBinder;
import android.text.Html;
import android.widget.Toast;
public class MyService extends Service {
public static String trackid;
ScheduledExecutorService scheduler ;
GPSTracker gps;
boolean flag=false;
DownloadFile ss;
Handler handler = new Handler();
double latin,longin,longitude,latitude;
private final int TEN_SECONDS = 3000;
#Override
public IBinder onBind(Intent arg0) {
return null;
}
String errors="notnull";
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
try
{
Toast.makeText(this, "Application Service Started!!!...", Toast.LENGTH_LONG).show();
try {
TimerTask myTimerTask = new TimerTask() {
#Override
public void run() {
flag=true;
DownloadFile sd=new DownloadFile();
sd.execute("");
} };
Timer timer = new Timer();
timer.schedule(myTimerTask, 1000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
Toast.makeText(this, "Service Error->"+e, Toast.LENGTH_LONG).show();}
return START_STICKY;
}
#Override
public void onDestroy()
{
flag=false;
super.onDestroy();
Toast.makeText(this, "Application Service Stopped!!!...", Toast.LENGTH_LONG).show();
}
public double getLatin() {
return latin;
}
public void setLatin(double latin) {
this.latin = latin;
}
public double getLongin() {
return longin;
}
public void setLongin(double longin) {
this.longin = longin;
}
public static String getTrackid() {
return trackid;
}
public static void setTrackid(String trackid) {
CopyOfMyService.trackid = trackid;
}
String pass;
private class DownloadFile extends AsyncTask<String, Integer, String> {
#Override
protected String doInBackground(String... sUrl) {
try {
gps = new GPSTracker(CopyOfMyService.this);
latin = gps.getLatitude();
longin = gps.getLongitude();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://iloadlogistics.com.au/insert1.jsp");
String trackid= CopyOfMyService.getTrackid();
List<NameValuePair> namevaluepairs = new ArrayList<NameValuePair>(2);
namevaluepairs.add(new BasicNameValuePair("trackid",trackid));
namevaluepairs.add(new BasicNameValuePair("lat",""+latin));
namevaluepairs.add(new BasicNameValuePair("lon",""+longin));
httppost.setEntity(new UrlEncodedFormEntity(namevaluepairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity rp = response.getEntity();
String origresponseText = EntityUtils.toString(rp);
String htmlTextStr = Html.fromHtml(origresponseText).toString();
pass=htmlTextStr.trim();
if(htmlTextStr.trim().equals("success"))
{
}else if(htmlTextStr.trim().equals("failure")){
}
} catch (Exception e) {
pass=""+e;
// TODO Auto-generated catch block
System.out.println("ERRRRRRRRRRRRRRRRRROR:"+e);
e.printStackTrace();
}
return pass;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(flag)
{
Toast.makeText(getApplicationContext(), "Inserted!!!...", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(), "Asyntask is Destroying!!!...", Toast.LENGTH_LONG).show();
onDestroy();
}
}
#Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
}
}
}
GPSTracker.java
public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
Location location; // location
double latitude; // latitude
double longitude; // longitude
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 5; // 10 meters
private static final long MIN_TIME_BW_UPDATES = 1000; // 1 minute
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
public void stopUsingGPS(){
if(locationManager != null){
locationManager.removeUpdates(GPSTracker.this);
}
}
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}
return latitude;
}
public double getLongitude(){
if(location != null){
longitude = location.getLongitude();
}
return longitude;
}
public boolean canGetLocation() {
return this.canGetLocation;
}
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
alertDialog.setTitle("GPS is settings");
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
public void onLocationChanged(Location location) {
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public IBinder onBind(Intent arg0) {
return null;
}
}
UPDATION 2
Instead of Timer i used to call the method scheduleSendLocation() which has the following definition given below
Runnable r=new Runnable() {
public void run() {
flag=true;
DownloadFile sd=new DownloadFile();
sd.execute("");
handler.postDelayed(this, TEN_SECONDS);
}
};
public void scheduleSendLocation() {
handler.postDelayed(r, TEN_SECONDS);
}
I don't really see what you need this handler for, but I see at least 2 problems:
you call new Handler() in the UI thread which has no prepared looper
you create a handler without overriding handleMessage() (what does this handler do then?)
When you create a new Handler with the constructor that has no parameter, the handler is associated with the message queue of the current thread (calling new Handler()). It means the thread needs a looper. You can do it by calling Looper.prepare() before creating the handler.
However, you can't do it yet because new Handler() is called as a field initialization. You should move this call somewhere else, such as in onCreate() and then add Looper.prepare() before it.