I am trying to send GPS coordinates to server in every 1 minute interval through a background service . It invokes the service for thee first time but it doesn't repeat it .
Code for setting the alarm:
Intent gpsIntent = new Intent(CheckInActivity.this, GoogleMapService.class);
gpsPendingIntent = PendingIntent.getService(CheckInActivity.this, 0, gpsIntent, 0);
AlarmManager alarmManager_gps = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar_gps = Calendar.getInstance();
calendar_gps.add(Calendar.SECOND, 20);
alarmManager_gps.setRepeating(AlarmManager.RTC_WAKEUP, calendar_gps.getTimeInMillis(), TimeUnit.MINUTES.toMillis(1), gpsPendingIntent);
Service
public class GoogleMapService extends Service {
private boolean gps_enabled = false;
private boolean network_enabled = false;
private static Location sLocation;
private LocationManager locManager;
private LocationListener locListener = new MyLocationListener();
private CallBack callback;
#Override
public void onCreate() {
}
#Override
public IBinder onBind(Intent intent) {
Log.w(null,"GPS Service called");
locManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
try {
gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
}
try {
network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
}
if (gps_enabled) {
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
}
if (network_enabled) {
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
}
callback = new CallBack() {
public void onResult(Boolean result) {
// TODO Auto-generated method stub
}
public void onProgress() {
// TODO Auto-generated method stub
}
};
return null;
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
#Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
if (location != null) {
// This needs to stop getting the location data and save the battery power.
locManager.removeUpdates(locListener);
sLocation = location;
Model.coordinates = location.getLatitude() + "," + location.getLongitude();
try{
SendGPSCoordinatesOperation task = new SendGPSCoordinatesOperation(callback);
task.execute("");
Log.w(null,"Sent");
}
catch(Exception e){
Log.w(null,"Couldn't be sent");
}
Log.w(null,"The coordinates are"+Model.coordinates);
}
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
}
EDIT : AndroidManifest code
Use this...
private void setLocationSendingAlarm() {
AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), GoogleMapService.class);
intent.putExtra("locationSendingAlarm", true);
PendingIntent pendingIntent = PendingIntent.getService(this, 987654321, intent,0);
try {
alarmManager.cancel(pendingIntent);
} catch (Exception e) {
}
int timeForAlarm=60000;
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+timeForAlarm, timeForAlarm,pendingIntent);
}
gpsPendingIntent = PendingIntent.getService(CheckInActivity.this, 0, gpsIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Related
I try to retrieve Location updates in Service by network provider, but the updates doesn,t perform. My code looks like this:
public class TimeService extends Service{
LocationManager lm;
LocationListener ll = new LocationListener()
{
#Override
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longtitude = location.getLongitude();
Intent inte =new Intent( TimeService.this, myAppWidgetProvider.class);
inte.setAction("action_location");
inte.putExtra("lat", latitude);
inte.putExtra("long", longtitude);
PendingIntent pinte = PendingIntent.getBroadcast(TimeService.this, 0, inte, 0);
try {
pinte.send();
} catch (CanceledException e) {
// TODO Auto-generated catch block
Log.d("sendPIException", ":-(");
}
Log.d("locReceived", "onLochanged");
}
#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
}
};
Log "locReceived" in onLocationChanged doesn't appear i logcat
Following code responsible for Location in my Service, too:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
this.registerReceiver(br, new IntentFilter(Intent.ACTION_TIME_TICK));
lm =(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, ll);
return super.onStartCommand(intent, flags, startId);
}
What's wrong in my code?
Criteria criteria = new Criteria();
String provider = lm.getBestProvider(criteria, false);
Location location = lm.getLastKnownLocation(provider);
lm.requestLocationUpdates(provider, 0, 0, ll);
if (location == null) {
Log.e("Impl", "Location not available");
}
I have litle problem with my app.
I get location from GPS or Network and everything works but...
if I get location from GPS and I turn off GPS from phone when I do not get location from Network
so.. I need help,
How to make when I turn off GPS switch on Network
my code:
public class Background_work extends Service implements LocationListener{
static String TAG="Service Created";
private Timer timer = new Timer();
private TimerTask task;
int count = 0;
LocationManager lm;
// Platuma ilguma
double lat,log;
//Tikrina kas ijungta
boolean GPSStatus = false;
boolean NetworkStatus = false;
public void setX(double X){
lat = X;
}
public void setY(double Y){
log = Y;
}
public double getX(){
return lat;
}
public double getY(){
return log;
}
public int onStartComand(Intent intent, int flags, int startId){
Toast.makeText(this, "Setver Started", Toast.LENGTH_LONG).show();
return Service.START_NOT_STICKY;
}
public void onCreate() {
super.onCreate();
Log.d(Background_work.TAG, "Service Created");
}
#Override
public void onStart(Intent intent, int startId) {
Log.d(Background_work.TAG, "Service started");
subscribeToLocationUpdates();
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
if(location != null){
setX(latitude);
setY(longitude);
}
}
#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
}
public void subscribeToLocationUpdates() {
this.lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
// GPS status
GPSStatus = this.lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
// Network status
NetworkStatus = this.lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Log.i("", "GPS" + GPSStatus + " network" + NetworkStatus);
if(GPSStatus == true){
this.lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 0, this);
}else if(NetworkStatus == true){
this.lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 0, this);
}else if(NetworkStatus == true && GPSStatus == true){
this.lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 0, this);
}
}
}
I am getting the "can't create handler inside thread that not called looper.prepare" error when trying to call,
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
However if I don't call it inside a thread( the run() ) it works fine..What should I do?
Edit:
MyAlarmService.java
private Handler handler = new Handler(){
#Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
System.out.println("Handle MSGGGGGGGGGG");
}
};
#Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
WorkerThreads WT1 = new WorkerThreads("Thread1");
WorkerThreads WT2 = new WorkerThreads("Thread2");
WT1.start();
WT2.start();
}
class WorkerThreads extends Thread{
public WorkerThreads(String string) {
// TODO Auto-generated constructor stub
super(string);
}
#Override
public void run() {
// TODO Auto-generated method stub
super.run();
if(getName().equals("Thread1")){
System.out.println("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
GetAlarmCoordinates gcc = new GetAlarmCoordinates();
gcc.init(getApplicationContext(),handler);
/*try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
System.out.println(MyAlarmService.currentLatLon[0] + " " + MyAlarmService.currentLatLon[1] );
}else if(getName().equals("Thread2")){
}
}
}
GetAlarmCoordinates.java
public void init(Context cont, Handler handler){
mHandler = handler;
locManager = (LocationManager) cont.getSystemService(Context.LOCATION_SERVICE);
onClick();
}
public void onClick() {
// progress.setVisibility(View.VISIBLE);
// exceptions will be thrown if provider is not permitted.
System.out.println("GetCurrentLocation");
try {
gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
}
try {
network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
}
System.out.println("gps_enabled : "+gps_enabled+ " network_enabled : "+network_enabled);
if (gps_enabled) {
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
}
if (network_enabled) {
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
}
}
class MyLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location location) {
if (location != null) {
// This needs to stop getting the location data and save the battery power.
locManager.removeUpdates(locListener);
//locationManager.requestLocationUpdates(GPS_PROVIDER, intervall, distance, listener);
latitude = location.getLatitude();
londitude = location.getLongitude();
MyAlarmService.currentLatLon[0] = latitude;
MyAlarmService.currentLatLon[1] = londitude;
System.out.println("Alarm Coodinates lat : "+latitude+" lon : "+londitude);
mHandler.sendEmptyMessage(0);
//notify();
}
}
You can use the Activity.runOnUiThread() method to avoid this exception, here's the snippet:
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
}
});
Hope this helps.
I'm guessing locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener); has to run in the UI thread.
Solutions:
Just run it in the UI thread without creating a new thread.
If you are running
`locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);`
from another class, then you can run the code via an activity instance:
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
// Your code
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
}
});
I hope this helps.
You can use getMainLooper in Context ,
just send context to your class as a parameter and user lopper
locationManager.requestLocationUpdates(getProviderName(), time, meter, locationUpdateListener,context.getMainLooper());
I am new user to Stackoverflow and this is my first question...
Question is...
How to get the periodic updates of location details? I have tried with Service class and run thread inside it but,This is called only once.
Can any one suggest me what I am doing wrong? Thank you.
Here is my code
public class MyService extends Service
{
String GPS_FILTER = "";
Thread triggerService;
LocationManager lm;
GpsListener gpsLocationListener;
boolean isRunning = true;
#Override
public void onCreate()
{
// TODO Auto-generated method stub
super.onCreate();
Toast.makeText(getApplicationContext(), "Hello",Toast.LENGTH_LONG).show();
GPS_FILTER = "MyGPSLocation";
}
#Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
triggerService = new Thread(new Runnable(){
public void run(){
try{
Looper.prepare();//Initialize the current thread as a looper.
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Toast.makeText(getApplicationContext(), "Hello1",Toast.LENGTH_LONG).show();
gpsLocationListener = new GpsListener();
long minTime = 30000; // 5 sec...
float minDistance = 10;
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime,
minDistance, gpsLocationListener);
Looper.loop();
}catch(Exception ex){
System.out.println("Exception in triggerService Thread -- "+ex);
}
}
}, "myLocationThread");
triggerService.start();
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
removeGpsListener();
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
private void removeGpsListener(){
try{
lm.removeUpdates(gpsLocationListener);
}
catch(Exception ex){
System.out.println("Exception in GPSService --- "+ex);
}
}
class GpsListener implements LocationListener{
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
double latitude = location.getLatitude();
double longitude = location.getLongitude();
sendBroadcast(filterRes);
postdata();
}**
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
}
My updated answer
This Call to service class
Calendar cur_cal = Calendar.getInstance();
cur_cal.setTimeInMillis(System.currentTimeMillis());
Intent intent = new Intent(this, MyService.class);
PendingIntent pintent = PendingIntent.getService(login.this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
public class MyService extends Service
{
String GPS_FILTER = "";
Thread triggerService;
LocationListener locationListener;
LocationManager lm;
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1000; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000*60; // in Milliseconds
protected LocationManager locationManager;
boolean isRunning = true;
#Override
public void onCreate()
{
// TODO Auto-generated method stub
super.onCreate();
GPS_FILTER = "MyGPSLocation";
// locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// locationManager.requestLocationUpdates(
// LocationManager.GPS_PROVIDER,
// MINIMUM_TIME_BETWEEN_UPDATES,
// MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
// new MyLocationListener());
}
#Override
public void onStart(Intent intent, int startId)
{
// TODO Auto-generated method stub
super.onStart(intent, startId);
turnGPSOn();
Toast.makeText(getApplicationContext(), "Hello1",Toast.LENGTH_LONG).show();
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATES, 1.0f, locationListener);
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
// removeGpsListener();
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
// private void removeGpsListener(){
// try{
// lm.removeUpdates(locationManager);
// }
// catch(Exception ex){
// System.out.println("Exception in GPSService --- "+ex);
// }
// }
private class MyLocationListener implements LocationListener
{
public void onLocationChanged(Location location)
{
postdata(location.getLatitude(),location.getLongitude());
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(MyService.this, message, Toast.LENGTH_LONG).show();
turnGPSOnOff();
}
public void onStatusChanged(String s, int i, Bundle b) {
// Toast.makeText(MyService.this, "Provider status changed",
// Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
// Toast.makeText(MyService.this,
// "Provider disabled by the user. GPS turned off",
// Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
// Toast.makeText(MyService.this,
// "Provider enabled by the user. GPS turned on",
// Toast.LENGTH_LONG).show();
}
}
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cur_cal.getTimeInMillis(), 60*1000, pintent);
Try Puuting things that you want to do inside run of this class,
This thread runs every 5 Seconds,
You can modify it as you want.
public class PeriodicChecker extends Thread
{
#Override
public void run()
{
while(true) {
System.out.println("Thread is doing something");
//Put Your code here
Thread.sleep(5000);
}
}
}
public OtherClass {
public static void main(String args[]) {
Thread t = new PeriodicChecker();
t.start();
}
}
I am using service class which gives a call to my inner class that implements Location Listener which provides me my location details... but I need to do this in regular interval.. I have used Thread with looper but it executes only once Please help
This is my udated answer
Updated answer
public class MyService extends Service
{
String GPS_FILTER = "";
Thread triggerService;
LocationListener locationListener;
LocationManager lm;
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1000; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000*60; // in Milliseconds
protected LocationManager locationManager;
boolean isRunning = true;
#Override
public void onCreate()
{
// TODO Auto-generated method stub
super.onCreate();
GPS_FILTER = "MyGPSLocation";
// locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// locationManager.requestLocationUpdates(
// LocationManager.GPS_PROVIDER,
// MINIMUM_TIME_BETWEEN_UPDATES,
// MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
// new MyLocationListener());
}
#Override
public void onStart(Intent intent, int startId)
{
// TODO Auto-generated method stub
super.onStart(intent, startId);
turnGPSOn();
Toast.makeText(getApplicationContext(), "Hello1",Toast.LENGTH_LONG).show();
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATES, 1.0f, locationListener);
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
// removeGpsListener();
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
// private void removeGpsListener(){
// try{
// lm.removeUpdates(locationManager);
// }
// catch(Exception ex){
// System.out.println("Exception in GPSService --- "+ex);
// }
// }
private class MyLocationListener implements LocationListener
{
public void onLocationChanged(Location location)
{
postdata(location.getLatitude(),location.getLongitude());
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(MyService.this, message, Toast.LENGTH_LONG).show();
turnGPSOnOff();
}
public void onStatusChanged(String s, int i, Bundle b) {
// Toast.makeText(MyService.this, "Provider status changed",
// Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
// Toast.makeText(MyService.this,
// "Provider disabled by the user. GPS turned off",
// Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
// Toast.makeText(MyService.this,
// "Provider enabled by the user. GPS turned on",
// Toast.LENGTH_LONG).show();
}
}
also call service using
Calendar cur_cal = Calendar.getInstance();
cur_cal.setTimeInMillis(System.currentTimeMillis());
Intent intent = new Intent(this, MyService.class);
PendingIntent pintent = PendingIntent.getService(login.this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cur_cal.getTimeInMillis(), 60*1000, pintent);
try this...
TimerTask doAsynchronousTask;
long timerCall = 5000;
final Handler handler = new Handler();
Timer timer = new Timer();
public static int times = 0;
// For tracking
doAsynchronousTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
performBackgroundTask.execute();//In this function add your code which you want to call repeatedly. OR ADD lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Toast.makeText(getApplicationContext(), "Hello1",Toast.LENGTH_LONG).show();
gpsLocationListener = new GpsListener();
long minTime = 30000; // 5 sec...
float minDistance = 10;
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDistance, gpsLocationListener);
Log.d("BGT", "BGT=" + times++);//how many time it called
} catch (Exception e) {
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, timerCall);// execute in every 50000 ms
`