2011-09-02 84 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

谢谢你的支持反馈...实施后,我会回到你身边...... –