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();
}
}
Related
I am new in android and recently start learning about Android and I am trying to get location of any android device by its unique android id. By that I can track the approx or exact location either by GPS or network provider. In detail I mean to say that whenever i enter any Android id in my app i can get device location in my application. Thanks for your kind help.
Finally I am able to do with this code:
public class MyService extends Service implements LocationListener{
String GPS_FILTER = "";
Thread triggerService;
LocationListener locationListener;
LocationManager lm;
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meter
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000 * 60 * 3; // 1 minute
protected LocationManager locationManager;
boolean isRunning = true;
Calendar cur_cal = Calendar.getInstance();
Location location;
double latitude; // latitude
double longitude;
UserFunctions userFunction;
private JSONObject json;
private AlertDialogManager alert = new AlertDialogManager();
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_ERROR_MSG = "error_msg";
private static String KEY_FLAG = "flag";
String android_id ;
String userName;
#Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Intent intent = new Intent(this, MyService.class);
PendingIntent pintent = PendingIntent.getService(getApplicationContext(),
0, intent, 0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
android_id = Settings.Secure.getString(getContentResolver(),
Settings.Secure.ANDROID_ID);
if (getAccount() != null) {
userName = getAccount();
}
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());
cur_cal.setTimeInMillis(System.currentTimeMillis());
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cur_cal.getTimeInMillis(),
60 * 1000*3, pintent);
}
#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(LOCATION_SERVICE);
locationListener = new MyLocationListener();
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, (LocationListener) this);
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
/*Toast.makeText(getApplicationContext(), latitude + ", ll" + longitude, Toast.LENGTH_LONG)
.show();*/
userFunction = new UserFunctions();
new YourAsyncTaskLogin().execute();
}
}
location =locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
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();
turnGPSOff();*/
}
private void postdata(double latitude, double longitude) {
// TODO Auto-generated method stub
/*Toast.makeText(getApplicationContext(),
latitude + ", " + longitude, Toast.LENGTH_LONG).show();*/
}
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();
}
}
public void turnGPSOn() {
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
this.sendBroadcast(intent);
String provider = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (!provider.contains("gps")) { // if gps is disabled
final Intent poke = new Intent();
poke.setClassName("com.android.settings",
"com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
this.sendBroadcast(poke);
}
}
// automatic turn off the gps
public void turnGPSOff() {
String provider = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (provider.contains("gps")) { // if gps is enabled
final Intent poke = new Intent();
poke.setClassName("com.android.settings",
"com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
this.sendBroadcast(poke);
}
}
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
class YourAsyncTaskLogin extends AsyncTask<Void, Void, Void> {
//private ProgressDialog _ProgressDialog;
#Override
protected void onPreExecute() {
// show your dialog here
/*_ProgressDialog = ProgressDialog.show(getApplicationContext(), "",
"Loading", true);*/
}
#Override
protected Void doInBackground(Void... params) {
json = userFunction.sendLocations(android_id, userName,latitude+"", longitude+"");
return null;
}
protected void onPostExecute(Void result) {
try {
Log.e("Key_Success:",
json.getString(KEY_SUCCESS));
if (json.getString(KEY_SUCCESS) != null) {
// loginErrorMsg.setText("");
String res = json.getString(KEY_SUCCESS);
if (Integer.parseInt(res) == 1) {
} else {
// Error in login
// loginErrorMsg.setText("Incorrect username/password");
//_ProgressDialog.cancel();
}
} else {
// Error in login
// loginErrorMsg.setText("Incorrect username/password");
//_ProgressDialog.cancel();
}
} catch (JSONException e) {
e.printStackTrace();
Log.e("error", e.getMessage());
}
//_ProgressDialog.dismiss();
}
}
public String getAccount() {
AccountManager manager = (AccountManager) getSystemService(ACCOUNT_SERVICE);
Account[] list = manager.getAccountsByType("com.google");
if (list.length != 0) {
String email = list[0].name;
return email;
} else {
return null;
}
}
}
if you want to get location of any device who has your app in their mobile , you can get it ,
firstly in your app you can upload locations to your server with memberid(you can set it , unique for every device) for every x time(you can set it how many times you update the location).
And then you can check your db on your server which device in where.
(my advice you can use webservice to update db on your server)
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 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);
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
`