Search code examples
androidservicecalllog

How to send data to server in android for every 12 hours in android?


Hi I'm developing an android application in which it will retrieve all the call log from the user mobile and store the data in SQLite database.Till here I have achieved but now I'm trying to send the data to my remote server for every 12 hours.I have created a service and a broadcast receiver but how to send the data to my remote server if the internet connection is detected?

This is my Service class

    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+"')");
    }

} 

This is my Broadcast Receiver class

   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;
}

Solution

  • For this you can use the AlarmManager that could start your service for every 12 Hours. In the service you can check the internet connection, if connection is available then connect to the server. For Example :

         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);
    

    for starting the staring the service at the mobile boot, Create a BroadcastReceiver and register it to receive ACTION_BOOT_COMPLETED. You also need RECEIVE_BOOT_COMPLETED permission.

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

    for more reference go through the API