2015-05-21 79 views
1

嗨我正在開發一個Android應用程序,它將從用戶手機中檢索所有通話記錄並將數據存儲在SQLite數據庫中。到此爲止我已經實現了,但現在我試圖將數據發送到遠程服務器每12小時。我創建了一個服務和一個廣播接收器,但如果檢測到互聯網連接,如何將數據發送到我的遠程服務器?如何在android中每12小時向Android中的服務器發送數據?

這是我的服務類

public class MyService extends Service { 

String callNumber,deviceId; 
int phoneType; 
String possibleEmail; 
Cursor c; 
SQLiteDatabase db; 

@Override 
public IBinder onBind(Intent intent) { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
public void onCreate() { 
    Toast.makeText(this, " MyService Created ", Toast.LENGTH_LONG).show(); 
} 

@Override 
public void onStart(Intent intent, int startId) { 
    Toast.makeText(this, " MyService Started", Toast.LENGTH_LONG).show(); 
    getAllCallLogs(); 
} 

@Override 
public void onDestroy() { 
    // TODO Auto-generated method stub 
    Toast.makeText(this, "Servics Stopped", Toast.LENGTH_SHORT).show(); 
    super.onDestroy(); 
} 

    private void getAllCallLogs(){ 

    TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE); 

    deviceId = tm.getDeviceId(); 
    phoneType = tm.getPhoneType(); 

    String strOrder = android.provider.CallLog.Calls.DATE + " DESC"; 
    Cursor cur = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, strOrder); 
    // loop through cursor 
    while (cur.moveToNext()) { 
     callNumber = cur.getString(cur 
       .getColumnIndex(android.provider.CallLog.Calls.NUMBER)); 

     String callName = cur 
       .getString(cur 
         .getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME)); 

     String callDate = cur.getString(cur 
       .getColumnIndex(android.provider.CallLog.Calls.DATE)); 
     SimpleDateFormat formatter = new SimpleDateFormat(
       "dd-MMM-yyyy HH:mm:ss"); 
     String dateString = formatter.format(new Date(Long 
       .parseLong(callDate))); 

     String callType = cur.getString(cur 
       .getColumnIndex(android.provider.CallLog.Calls.TYPE)); 

     String type = null; 
     int type1 = Integer.parseInt(callType); 

     switch(type1){ 

     case CallLog.Calls.OUTGOING_TYPE: 
       type = "OUTGOING"; 
       break; 

     case CallLog.Calls.INCOMING_TYPE: 
       type = "INCOMING"; 
       break; 

     case CallLog.Calls.MISSED_TYPE: 
       type = "MISSED"; 
       break; 
     } 
     String duration = cur.getString(cur 
       .getColumnIndex(android.provider.CallLog.Calls.DURATION)); 

     // process log data... 
     Toast.makeText(getApplicationContext(), ""+type, 5000).show(); 
     db = this.openOrCreateDatabase("call_log", MODE_PRIVATE, null); 
     db.execSQL("insert into call_log(call_name,call_number,call_date,call_type,call_duration,device_id,user_name)values('"+callName+"','"+callNumber+"','"+dateString+"','"+type+"','"+duration+"','"+deviceId+"','"+possibleEmail+"')"); 
    } 

} 

這是我的廣播接收器

public class MyBroadcast extends BroadcastReceiver{ 

@Override 
public void onReceive(Context context, Intent intent) { 
    // TODO Auto-generated method stub 
    if(isNetConnected(context)){ 
     Toast.makeText(context, "Connected",5000).show(); 

    } 
    else 
    { 
     Toast.makeText(context, "Not Connected", 5000).show(); 
    } 
} 

public static boolean isNetConnected(Context context){ 
    ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    if (connectivity != null) 
    { 
     NetworkInfo[] info = connectivity.getAllNetworkInfo(); 
     if (info != null) 
      for (int i = 0; i < info.length; i++) 
       if (info[i].getState() == NetworkInfo.State.CONNECTED) 
       { 
        return true; 
       } 

    } 
    return false; 
} 

回答

1

爲此,您可以使用AlarmManager,可以開始你的服務,爲每12小時。在服務中,您可以檢查互聯網連接,如果連接可用,則連接到服務器。 例如:

 AlarmManager mgr = (AlarmManager) context 
        .getSystemService(Context.ALARM_SERVICE); 
    Intent notificationIntent = new Intent(context, 
        UpdateService.class); 
    PendingIntent pendingIntent=PendingIntent.getService(context, requestCode, Intent.parseIntent(), 0); 
    mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, 
      System.currentTimeMillis(), AlarmManager.INTERVAL_HALF_DAY, pendingIntent); 

用於在移動引導啓動凝視該服務, 創建BroadcastReceiver並註冊以接收ACTION_BOOT_COMPLETED。您還需要RECEIVE_BOOT_COMPLETED權限。

public class MyReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     //register the alarm manager. 
    } 
} 

更多參考經過API

+0

即使移動再次重新啓動,我的服務應該在後臺運行如何實現? – Sunil

+0

更新了答案,檢查它。 – Kartheek

0

你必須使用服務,告警管理。 以秒爲單位設置時間間隔12 * 60 * 60。

說明和代碼示例有: https://developer.android.com/training/scheduling/alarms.html

+0

即使艱難,如果手機重新啓動再我的服務應該在後臺運行,如何實現這一目標? – Sunil

+0

你必須使用intent「android.permission.RECEIVE_BOOT_COMPLETED」並通過intent調用重新運行你的鬧鐘。 這裏的示例:https://developer.android.com/training/scheduling/alarms.html#boot – Vlad

相關問題