2012-11-24 106 views
0

我有一個BroadcastReceiver它將監視我的連接到Wifi或移動網絡的狀態。 BroadcastReceiver的整個要點是在手機建立連接時訪問SQLite DatabaseBroadcastReceiver被稱爲無限次

所以在我方法,我註冊了Reciever:

networkMonitor = new CaseQueueReceiver(); 
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);   
registerReceiver(networkMonitor, filter); 

,並在我的onDestory方法,我註銷它

unregisterReceiver(networkMonitor); 

在這個networkMonitor接收機

public class CaseQueueReceiver extends BroadcastReceiver { 

    public boolean available; 
    DatabaseHandler db; 
    QueueDB queueDB; 
    HashMap<String, String> queueHashMap; 

    public CaseQueueReceiver() { 
     db = new DatabaseHandler(ContextHelper.context()); 
     queueDB = new QueueDB(ContextHelper.context()); 
     queueHashMap = new HashMap<String, String>(); 
    } 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) { 
      NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO); 
      String typeName = info.getTypeName(); 
      String subtypeName = info.getSubtypeName(); 
      available = info.isAvailable(); 
      Log.i("Network Monitor", "Network Type: " + typeName 
       + ", subtype: " + subtypeName 
       + ", available: " + available); 

      //call a method which will get all the unsent cases from the database, and update their field of sent status 
     //in order to do so, add an extra column in the database, also remember to delete the cases.  

     if(available) { 

      int count = queueDB.countUnsentCases(); 
      Log.i("Count unsentCases: ", Integer.toString(count)); 
      queueDB.getUnsetCases(); 

      // Iterator<Entry<String, String>> it = queueHashMap.entrySet().iterator(); 
//   while (it.hasNext()) { 
//     Map.Entry pairs = (Map.Entry)it.next(); 
//     Log.i("In the Queue: ", "PCN: " + pairs.getKey() + " Nist-File: " + pairs.getValue()); 
//   } 

     } 
     } 
    } 

而我的兩個方法來自QueueDB

public int countUnsentCases() { 

     String SQLQuery = "SELECT COUNT(" + PCN + ") FROM " 
       + TABLE_CASES_IN_QUEUE + ";"; 
     SQLiteDatabase db = this.getWritableDatabase(); 

     Cursor cursor = db.rawQuery(SQLQuery, null); 
     cursor.moveToFirst(); 
     int count = cursor.getInt(0); 
     cursor.close(); 
     db.close(); 

     return count; 
    } 

    public HashMap<String, String> getUnsetCases() { 

     HashMap<String, String> queueHashMap = new HashMap<String, String>(); 

     SQLiteDatabase db = this.getWritableDatabase(); 
     String query = "SELECT * FROM " + TABLE_CASES_IN_QUEUE + ";"; 
     Cursor cursor = db.rawQuery(query, null); 

     if (cursor.moveToFirst()) { 
      do { 

       Log.i("CURSOR(0)", cursor.getString(0)); 
       // queueHashMap.put(cursor.getString(0), cursor.getString(1)); 
      } while (cursor.moveToNext()); 
     } 
     cursor.close(); 
     db.close(); 

     return queueHashMap; 
    } 

我遇到的問題是onReceive方法在打開Wifi時會調用無限。這不會導致任何異常,我的應用程序將掛起,並吃堆的記憶。我知道我應該在線程中讀/寫數據庫。任何人都可以解釋爲什麼這個onReceive方法會被多次調用?解決這個問題的最好方法是什麼?

在此先感謝!

+0

讓線程先走。你的問題可能是因爲你*捆綁了東西,Android檢測到這一點,並嘗試重新發送廣播。 – CommonsWare

回答

2
public static final String CONNECTIVITY_ACTION 

Added in API level 1 
A change in network connectivity has occurred. A connection has either been established or lost. The NetworkInfo for the affected network is sent as an extra; it should be consulted to see what kind of connectivity event occurred. 

If this is a connection that was the result of failing over from a disconnected network, then the FAILOVER_CONNECTION boolean extra is set to true. 

For a loss of connectivity, if the connectivity manager is attempting to connect (or has already connected) to another network, the NetworkInfo for the new network is also passed as an extra. This lets any receivers of the broadcast know that they should not necessarily tell the user that no data traffic will be possible. Instead, the reciever should expect another broadcast soon, indicating either that the failover attempt succeeded (and so there is still overall data connectivity), or that the failover attempt failed, meaning that all connectivity has been lost. 

For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY is set to true if there are no connected networks at all. 

Constant Value: "android.net.conn.CONNECTIVITY_CHANGE"