Android PhoneStateListener:待机模式下的onCellLocationChanged
我正在编写一个在后台作为前台服务运行的应用程序。该服务实例化 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 上进行测试
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这种情况是屏幕关闭,因此手机进入睡眠模式。
查看 PowerManager。
定义了以下标志,对系统功率有不同的影响。这些标志是互斥的 - 您只能指定其中之一。
*如果您持有部分唤醒锁,CPU 将继续运行,无论任何计时器如何,甚至在用户按下电源按钮后也是如此。在所有其他唤醒锁中,CPU 将运行,但用户仍然可以使用电源按钮使设备进入睡眠状态。
This situation is screen off thus, phone goes into sleep mode.
Take a look into PowerManager.
The following flags are defined, with varying effects on system power. These flags are mutually exclusive - you may only specify one of them.
*If you hold a partial wakelock, the CPU will continue to run, irrespective of any timers and even after the user presses the power button. In all other wakelocks, the CPU will run, but the user can still put the device to sleep using the power button.