Why is my scheduleAtFixedRate being ignored in my code..?
Greg
Here is my New code based on your suggestions. It did not work. I got a bunch of "Just sent a text message with coords" in a row. So it still is not sleeping for 15 seconds.
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; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.os.Handler; 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 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("In initService..Droid Service...!!"); // 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(); // } // }; // 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);
}
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)
{
}
}
}
Here is my code
I get "Just sent a text message with coords" sent like once a second even though my scheduleAtFixedRate is set for 10 seconds.
I got this to work in a normal program, but on the Android platform, they want you to use Looper....and it appears that my code is caught up in this Looper so therefore it is ignoring my scheduleAtFixedRate(10 seconds)
Any help is greatly appreciated.
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)
{
}
}
}
I think the misconception here is not so much that Android wants to use a Looper, but thaty you shouldn't use the Timer for the behavior that you desire. Instead of the Timer you have to manipulate a Looper. I haven't used the Looper directly in a while because there are other components which abstract away a lot of the nuances of it. However I think that Looper.loop() should not return until an outside entity either interrupts your thread or quit's the looper for you. So I don't think that will work, I could be wrong though.
You could use a Handler if you want to create a pulse. A Handler allow you to do work on the Looper thread. So with the example you have,
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() {
updateNotication();
mHandler.postDelayed(this, PERIOD);
}
};
}
I will admit from here on out I am only guessing I know the desired outcome of your application. I am assuming you don't need two way communication as your IBinder is null.
@Override
public void onStart() {
mRunnable.run();
}
// The rest of your code.
..
尝试从onStart()调用initService。
Basically the best bet for me was to call this onCreate. Now it is doing exactly what I want it to do. Thanks for all your inputs..!! This helped me solve the problem.
链接地址: http://www.djcxy.com/p/74934.html上一篇: 定时器与回调