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

不管怎樣,謝謝您的關注。