Android PhoneStateListener: onCellLocationChanged in standby

I am writing an application that runs in the background as a foreground service. This service creates an instance of the PhoneStateListener subclass. PhoneStateListener creates a TelephonyService with a foreground service context object, and then listens for cell location changes. This works great while the display is on. But as soon as the display goes blank, cell registration no longer works.

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 the phone screen is on, everything works fine, but when the onCellLocationChanged event enable screen never rings. I also try GsmCellLocation currentCellLocation = (GsmCellLocation) p_TelephonyManager.getCellLocation (); this code always returns the last cell identifier before the screen is turned off, and not the actual cell identifier.

I also 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'm doing wrong? Testing on HTC Desire Z Android 2.2

+4
source share
1 answer

This situation is turned off, the phone goes into sleep mode. Take a look at PowerManager .

The following flags are defined with various effects on system power. These flags are mutually exclusive - you can specify only one of them.

 Flag Value CPU Screen Keyboard PARTIAL_WAKE_LOCK On* Off Off SCREEN_DIM_WAKE_LOCK On Dim Off SCREEN_BRIGHT_WAKE_LOCK On Bright Off FULL_WAKE_LOCK On Bright Bright 

* If you have a partial wakelock, the processor will continue to work regardless of any timers and even after the user presses the power button. In all other wakelocks, the processor will work, but the user can still turn off the device using the power button.

  PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag"); wl.acquire(); ..screen will stay on during this section.. wl.release(); 
0
source

All Articles