Android PhoneStateListener:待机模式下的onCellLocationChanged

时间:2011-04-20 06:48:52

标签: android cellid

我正在编写一个在后台运行的应用程序作为前台服务。此服务实例化PhoneStateListener的子类。 PhoneStateListener使用前台服务的上下文对象创建TelephonyService,然后侦听单元位置更改。当显示在中时,这完美地工作。但是一旦显示器熄灭,单元格的记录就不再起作用了。

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" />

当手机屏幕打开时,一切正常,但是当onCellLocationChanged事件的转动画面从不调用时。 我也试试 GsmCellLocation currentCellLocation =(GsmCellLocation)p_TelephonyManager .getCellLocation(); 通过此代码始终返回屏幕前的最后一个单元格ID,而不是实际的单元格ID。

我也在服务中尝试partial_wake_lock,但结果相同:

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;
        }
...
}

知道我做错了什么吗?测试HTC Desire Z Android 2.2

1 个答案:

答案 0 :(得分:0)

因此屏幕关闭,手机进入睡眠模式。 看看PowerManager

定义了以下标志,对系统功率产生不同的影响。这些标志是互斥的 - 您只能指定其中一个。

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

*如果您持有部分唤醒锁,CPU将继续运行,无论是否有任何计时器,甚至在用户按下电源按钮之后。在所有其他唤醒锁中,CPU将运行,但用户仍然可以使用电源按钮使设备进入睡眠状态。

 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();