2013-01-24 15 views
1

我已經創建了一個BroadcastReceiver來監聽他的電話狀態並採取相應的行動。PhoneStateListener和BroadcastReceiver

這裏的問題是,它似乎並沒有從我的數據庫採取正確的價值觀,並從一開始就處於閒置狀態敬酒出現了兩次,並顯示出四倍等第二個電話後...

我想有什麼毛病我的課,但我想不出什麼...

下面的代碼片段:

public class PhoneListener extends BroadcastReceiver{ 

    private DBAdapter db; 
    private Cursor  data; 
    private Cursor  options; 
    private int  activer; 
    private int  mouvement; 
    private int  verticalite; 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 
     telephonyManager.listen(new CustomPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE); 

    } 

    public void stopService(Context context){ 
     Intent alerte = new Intent(context, SensorOrientation.class); 
     alerte.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     context.stopService(alerte); 
    } 

    public void startService(Context context){ 
     Intent alerte = new Intent(context, SensorOrientation.class); 
     alerte.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     context.startService(alerte); 
    } 

    public void startnotif(Context context){ 
     Intent intent = new Intent(context, SecuriteHauteReboot.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     context.startService(intent); 
    } 

    public class CustomPhoneStateListener extends PhoneStateListener { 

     //private static final String TAG = "PhoneStateChanged"; 
     Context context; //Context to make Toast if required 
     public CustomPhoneStateListener(Context context) { 
      super(); 
      this.context = context; 
     } 

     @Override 
     public void onCallStateChanged(int state, String incomingNumber) { 
      super.onCallStateChanged(state, incomingNumber); 

      switch (state) { 
      case TelephonyManager.CALL_STATE_IDLE: 
       db   = new DBAdapter(context); 
       data  = db.selectWidget(); 
       options  = db.selectVerticalite(); 
       activer  = data.getInt(data.getColumnIndex("activer")); 
       mouvement = options.getInt(options.getColumnIndex("activer_alarme_mouvement")); 
       verticalite = options.getInt(options.getColumnIndex("activer_alarme_verticalite")); 
       data.close(); 
       options.close(); 
       //when Idle i.e no call 
       Toast.makeText(context, 
         String.format(" "+mouvement+" "+activer), 
         Toast.LENGTH_SHORT).show(); 
       if(activer == 1){ 
        if(mouvement == 1 || verticalite == 1){ 
         Toast.makeText(context, 
           String.format("and I'm here...."), 
           Toast.LENGTH_SHORT).show(); 
         startService(context); 
         startnotif(context); 
        } 
       } 
       break; 
      case TelephonyManager.CALL_STATE_OFFHOOK: 
       //when Off hook i.e in call 
       //Make intent and start your service here 
       if (activer == 1){ 
        if(mouvement == 1 || verticalite == 1){ 
         stopService(context); 
        } 
       } 
       break; 
      case TelephonyManager.CALL_STATE_RINGING: 
       //when Ringing 
       if (activer == 1){ 
        if(mouvement == 1 || verticalite == 1){ 
         Toast.makeText(context, 
           String.format("and I'm here...."), 
           Toast.LENGTH_SHORT).show(); 
         stopService(context); 
        } 
       } 
       break; 
      default: 
       break; 
      } 
     } 
    } 
} 

回答

2

你可能會做給許多onCallStateChanged(數據庫裏面我/ O操作)。我在BroadcastReceiver這裏有類似的問題:Changing ringer volume during ringing。要進行測試,請嘗試在BroadcastReceiver內部對靜態字段(而不是LiteSQL)進行操作,並檢查是否有幫助。

+0

謝謝soulreaver!這正是問題所在。我不喜歡使用太多的靜態值,所以我只是創建服務,它的作用就像一個魅力!問題解決了 :) – MademoiselleLenore