I'm writing an app that runs in the background as a foreground service. This service instantiates a subclass of PhoneStateListener. The PhoneStateListener creates a TelephonyService with a context object of the foreground service and then listens to cell location changes. This works perfectly while the display in on. But as soon as the display goes off, the logging of the cells doesn't work anymore.
public class gps_service extends Service
{
public static TelephonyManager p_TelephonyManager = null;
public static myPhoneStateListener p_myPhoneStateListener = null;
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
p_myPhoneStateListener = new myPhoneStateListener();
p_TelephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
p_TelephonyManager.listen(p_myPhoneStateListener, PhoneStateListener.LISTEN_CELL_LOCATION);
...
}
public class myPhoneStateListener extends PhoneStateListener
{
public static int CurrentCellID;
/*
public static int current_cell_id;
@Override
public void onCellLocationChanged(CellLocation p_CellLocation)
{
//write to log
}
}
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
When phone screen is on, all working fine, but when turn screen of onCellLocationChanged event never call. Also I try GsmCellLocation currentCellLocation = (GsmCellLocation) p_TelephonyManager .getCellLocation(); by this code always return last cell id before off screen, not actual cell id.
Also I try partial_wake_lock in my service, but the same results:
private PowerManager pPowerManager = null;
private PowerManager.WakeLock pWakeLock = null;
..
@Override
public void onCreate()
{
super.onCreate();
pPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
pWakeLock = pPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "No sleep");
pWakeLock.acquire();
...
}
@Override
public void onDestroy()
{
if (pWakeLock != null)
{
pWakeLock.release();
pWakeLock = null;
}
...
}
Any idea what I do wrong? Testing on HTC Desire Z Android 2.2