2011-09-02 215 views
0

其實我已經寫了一個使用web服務來更新服務器數據庫的方法,從我的應用程序安裝在使用兩個IP地址的設備中。如果一個IP失敗,那麼它使用第二個IP來在服務器上提取數據。 如果兩個IP地址都失敗了,我將數據保存到我的一個sqllite數據庫表tblTransaction中。下面給出了代碼。需要後臺運行進程

private void Delay15Minute() throws IOException { 
     String server1IPAddress = ""; 
     String server2IPAddress = ""; 
     String deviceId = ""; 
     Cursor cursorAdmin; 
     Cursor cursorTransaction; 
     adminhelper = new admin_helper(this); 
     cursorAdmin = adminhelper.GetAdminDetails(); 
     if (cursorAdmin.moveToFirst()) 
      server1IPAddress = cursorAdmin.getString(cursorAdmin 
        .getColumnIndex("RemoteServer1IPAddress")); 
     server2IPAddress = cursorAdmin.getString(cursorAdmin 
       .getColumnIndex("RemoteServer2IPAddress")); 
     deviceId = cursorAdmin.getString(cursorAdmin 
       .getColumnIndex("DeviceID")); 
     cursorAdmin.close(); 




     ContentValues initialDelay15 = new ContentValues(); 
     ContentValues initialTransaction = new ContentValues(); 
     Date date = new Date(); 
     SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy"); 
     String RevisedEstimatedDate = sdf.format(date); 



     manifest_helper = new manifest_helper(this); 
     cursor = manifest_helper.GetDeliveries(pkManifest); 
     cursor.moveToFirst(); 
     dbAdapter = new DatabaseAdapter(this); 
     dbAdapter.open(); 

     for (int i = 0; i < cursor.getCount(); i++) { 
      cursor.getString(cursor.getColumnIndex("PKDelivery")); 
      String RevisedTime=cursor.getString(cursor.getColumnIndex("RevisedEstimatedDeliveryTime"));  

      // get hour and minute from time string 
      StringTokenizer st1 = new StringTokenizer(RevisedTime, ":"); 
      int j = 0; 
      int[] val = new int[st1.countTokens()]; 
      // iterate through tokens 
      while (st1.hasMoreTokens()) { 
       val[j] = Integer.parseInt(st1.nextToken()); 
       j++; 
      } 

      // call time add method with current hour, minute and minutesToAdd, 
      // return added time as a string 
      String dateRevisedEstimatedDeliveryTime = addTime(val[0], val[1], 15); 
      initialDelay15.put("RevisedEstimatedDeliveryTime", 
        dateRevisedEstimatedDeliveryTime); 


      dbAdapter.UpdateRecord("tblDelivery", initialDelay15, "PKDelivery" 
      + "=" + cursor.getString(cursor.getColumnIndex("PKDelivery")), null); 


     } 
     dbAdapter.close(); 
     dataXmlExporter=new DataXmlExporter(this); 
     dataXmlExporter.StartDataSet();  
     cursor = manifest_helper.GetDeliveries(pkManifest); 
     dataXmlExporter.AddRowandColumns(cursor,"tblDelivery"); 

     String sqlTransaction = "Select 6 as TransactionType,'Update Revised Estimated Delivery Time' as Description," 
       + " deviceId as DeviceID ,date() as TransactionUploadDate,time() as TransactionUploadTime from tblAdmin where PKAdmin > ?"; 

     dbAdapter = new DatabaseAdapter(this); 
     dbAdapter.open(); 
     cursorTransaction = dbAdapter.ExecuteRawQuery(sqlTransaction, "-1"); 
     dataXmlExporter.AddRowandColumns(cursorTransaction, "Transaction"); 



     String XMLTransactionData=dataXmlExporter.EndDataSet();  

     try { 

      if ((server1IPAddress != "") && (server2IPAddress != "")) { 
       try { 
        if (server1IPAddress != "") { 
         InsertUploadedTrancasctionDetails(server1IPAddress, 
           deviceId, XMLTransactionData); 
        } 
       } catch (Exception exception) { 

        if ((server1IPAddress != server2IPAddress) 
          && (server2IPAddress != "")) { 
         InsertUploadedTrancasctionDetails(server2IPAddress, 
           deviceId, XMLTransactionData); 
        } 
       } 

      } 
     } catch (Exception exception) { 

      initialTransaction.put("ReceivedDate", 
        RevisedEstimatedDate); 
      initialTransaction.put("TransactionData", 
        XMLTransactionData);    
      dbAdapter.InsertRecord("tblTransaction", "", 
        initialTransaction); 



     } 
     dbAdapter.close(); 

     LoadDeliveries(pkManifest); 
    } 

的問題是,我需要更新的數據存儲在tbltransaction服務器自動當我們與我跑步相處連接到服務器IP application.I認爲這是可能通過建立底色運行方式與我的應用程序一起檢查tbltransaction中是否存在數據,並與服務器建立連接。

所以將任何一個有這種想法......如果是這樣,請幫助meee ...........

回答

0

這裏有一對夫婦一起將做到這此方法。這些都在一個服務中,您可以使用一個處理程序,並在一個活動中執行此操作,但服務是一個明智的選擇。

首先,我們需要能夠告訴我們是否在線,這需要網絡狀態和Wifi國家權限:

public boolean isOnline() { 
    try { 
     ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
     return cm.getActiveNetworkInfo().isConnectedOrConnecting(); 
    } catch (Exception e) { 
     return false; 
    } 
} 

接下來,我們需要能夠設置鬧鐘重試。添加這些變量:

private static AlarmManager am; 
private static PendingIntent pIntent; 
public static final int MSG_UPDATE = 0; 
public static final String PENDING_REQ_KEY = "request"; 

而設定的報警方法:

private synchronized void setAlarm() { 

    if (am == null) am = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
    if (Constants.LOG_DEBUG) Log.d(TAG,"Setting Alarm for 5 mins"); 
    long delay = 300000L; //5 Mins 
    Intent i = new Intent(this,Service.class); //Reference the Service this method is in 
    i.putExtra(PENDING_REQ_KEY, MSG_UPDATE); 
    pIntent = PendingIntent.getService(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT); 
    am.set(AlarmManager.RTC,System.currentTimeMillis()+delay,pIntent); 

} 

接下來,重寫onStartCommand方法,攻克了更新的要求:

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    if (intent.getExtras() != null && intent.getExtras().containsKey(PENDING_REQ_KEY)) { 
     int request = intent.getExtras().getInt(PENDING_REQ_KEY); 
     if (request == MSG_UPDATE) update(); 
    } 
    return START_STICKY; 
} 

最後,您更新方法:

public void update() { 
    if (isOnline()) { 
     /** Push data to server here */ 
    } 
    else { 
     setAlarm(); 
    } 
} 
+0

謝謝你的支持反饋...實施後,我會回到你身邊...... –