1

我已经开发了广播接收器侦听以这种方式在清单中声明的​​手机信号强度 听signalStrength当手机睡觉

<receiver android:name="it.cazzeggio.android.PhoneStateListener" > 
    <intent-filter android:priority="999" > 
     <action android:name="android.intent.action.SIG_STR" /> 
    </intent-filter> 
</receiver> 

的Java代码

public class PhoneStateListener extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
    Log.e(PhoneStateListener.class.getSimpleName(), new Date().toString()); 
    try{ 
     TelephonyManager telephony = (TelephonyManager) 
      context.getSystemService(Context.TELEPHONY_SERVICE); 

     //...some checks to be sure that is a gsm-event.. 

     GsmCellLocation location = (GsmCellLocation) telephony.getCellLocation(); 
     foundCells.add(0,new String[] { 
      telephony.getNetworkOperator() + "_" + location.getLac() + "_" + 
       location.getCid() , ""+(bundle.getInt("GsmSignalStrength")+1)}); 
     if(!foundCells.isEmpty()) 
      Functions.CellHistory.addHistory(foundCells); 
    }catch (Exception e) { 
     Log.e(PhoneStateListener.class.getSimpleName(), e.getMessage(), e); 
    } 
} 

如果屏幕是一切正常,但当手机进入睡眠模式 我的接收器停止工作(=没有事件被分派到onReceive方法)

我已经尝试注册接收器作为服务或使用PARTIAL_WAKE_LOCK没有结果(我是一个新手)。任何解决方案

在此先感谢

+0

你的BroadcastReceiver类的代码是什么?包括在这里。 – 2013-02-23 11:00:35

+0

你能发布你的PhoneStateListener类代码吗? – Riskhan 2013-02-23 11:09:41

回答

1

玉家伙,在谷歌上搜索网我发现,是一个悬而未决的问题的android: 只是为了节省电池,当屏幕关闭时,电话停止更新所有听众 有关信号强度。所以现在我放弃了。

我只是做一个愚蠢的解决方法,以获得至少小区ID的手机连接到: 在清单我定义服务

<service android:name="it.cazzeggio.android.util.OffScreenPhoneListener"/> 

该服务将在的onCreate方法来开始我的当应用程序启动

startService(new Intent(this, OffScreenPhoneListener.class)); 
在类

OffScreenPhoneListener的“的onCreate”方法开始 定时器周期性地重复对手机信号塔的检查主要活动

PowerManager powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE); 
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, 
    OffScreenPhoneListener.class.getSimpleName()); 
if(!wakeLock.isHeld()) 
    wakeLock.acquire(); 
timer=new Timer(); 
timer.schedule(new myTimerTask(), DELAY, DELAY); 

myTimerTask TimerTask的延伸,并在其方法:

TelephonyManager telephony = (TelephonyManager) 
    getBaseContext().getSystemService(Context.TELEPHONY_SERVICE); 
GsmCellLocation location = (GsmCellLocation) telephony.getCellLocation(); 
//Adding to my history the following infos: 
// telephony.getNetworkOperator() 
// location.getLac() 
// location.getCid() 

的方法的onDestroy清除所有我做的东西:

super.onDestroy(); 
timer.cancel(); 
timer.purge(); 
if(wakeLock!=null && wakeLock.isHeld()) 
    wakeLock.release(); 

不管怎样,谢谢您的关注。