GregPourquoi mon scheduleAtFixedRate est-il ignoré dans mon code ..?
Voici mon nouveau code basé sur vos suggestions. Cela n'a pas fonctionné. J'ai eu un tas de "juste envoyé un message texte avec des coords" dans une rangée. Donc, il ne dort toujours pas pendant 15 secondes.
package com.droid.service;
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Timer; import java.util.TimerTask;
import android.app.Service; import android.content.Context; import android.content.Intent; importer android.location.Location; import android.location.LocationListener; import android.location.LocationManager; importez android.os.Bundle; importer android.os.Handler; importer android.os.IBinder; importez android.os.Looper;
DroidService public class étend son service {
private LocationManager lm;
private LocationListener locationListener;
private Location location = null;
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
// @Override
// public void onCreate()
// {
// super.onCreate();
// initService();
// }
private static final int PERIOD = 15000;
Handler mHandler;
Runnable mRunnable;
@Override
public void onCreate()
{
super.onCreate();
mHandler = new Handler();
mRunnable = new Runnable()
{
public void run()
{
updateNotification();
mHandler.postDelayed(this, PERIOD);
System.out.println("PAUSE FOR 15 SECONDS..!!");
}
};
}
@Override
public void onStart(final Intent intent, final int startId)
{
super.onStart(intent, startId);
mRunnable.run();
}
// private void InitService() {// // System.out.println ("En service initService..Droid ... !! "); // int initialDelay = 15000; // commence après 15 secondes // // int period = 300000; // répète toutes les 5 minutes // // int period = 1800000; // répète toutes les 30 minutes // int period = 15000; // répète toutes les 15 secondes pour les tests // Minuterie minuterie = new Minuterie(); // tâche TimerTask = new TimerTask() // { // public void run() // { // Looper.prepare(); // updateNotification(); // Looper.loop(); //} //}; // timer.scheduleAtFixedRate (tâche, initialDelay, point); //}
protected void updateNotification()
{
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
public static class DateUtils
{
public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
public static String now()
{
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
return sdf.format(cal.getTime());
}
}
private class MyLocationlistener implements LocationListener
{
public void onLocationChanged(Location loc)
{
double lat = loc.getLatitude();
double lon = loc.getLongitude();
String latitude = Double.toString(lat);
String longitude = Double.toString(lon);
String coords = latitude + longitude;
// comment out text message for debug mode
// SmsManager sm = SmsManager.getDefault();
// sm.sendTextMessage("phoneNumber", null, coords, null, null);
System.out.println("Just sent a text message with coords");
}
public void onProviderDisabled(String provider)
{
}
public void onProviderEnabled(String provider)
{
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
}
Voici mon code
je reçois "viens juste d'envoyer un message texte avec coords" envoyé comme une fois par seconde, même si mon scheduleAtFixedRate est réglé pendant 10 secondes. J'ai obtenu ceci pour fonctionner dans un programme normal, mais sur la plateforme d'Android, ils veulent que vous utilisiez Looper .... et il semble que mon code soit pris dans ce Looper donc donc il ignore mon scheduleAtFixedRate (10 secondes)
Toute aide est grandement appréciée.
package com.droid.service;
import java.util.Timer;
import java.util.TimerTask;
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.os.Looper;
public class DroidService extends Service
{
private LocationManager lm;
private LocationListener locationListener;
private Location location = null;
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
@Override
public void onCreate()
{
super.onCreate();
initService();
}
private void initService()
{
System.out.println("In initService..!!");
int initialDelay = 15000; // start after 15 seconds
// int period = 300000; // repeat every 5 minuets
// int period = 1800000; // repeat every 30 minuets
int period = 15000; // repeat every 15 seconds for testing
Timer timer = new Timer();
TimerTask task = new TimerTask()
{
public void run()
{
Looper.prepare();
updateNotification();
Looper.loop();
Looper.myLooper().quit();
}
};
timer.scheduleAtFixedRate(task, initialDelay, period);
}
protected void updateNotification()
{
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
private class MyLocationlistener implements LocationListener
{
public void onLocationChanged(Location loc)
{
double lat = loc.getLatitude();
double lon = loc.getLongitude();
String latitude = Double.toString(lat);
String longitude = Double.toString(lon);
String coords = latitude + longitude;
// comment out text message for debug mode
// SmsManager sm = SmsManager.getDefault();
// sm.sendTextMessage("phoneNumber", null, coords, null, null);
System.out.println("Just sent a text message with coords");
}
public void onProviderDisabled(String provider)
{
}
public void onProviderEnabled(String provider)
{
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
}
L'appel de onStart() m'a donné le même résultat. – Biggs
Avez-vous pu obtenir un point d'arrêt dans la méthode onStart lors du débogage? – Vinay