android - Syncing offline data sequentially whenever internet is available -
i'm working on project collects user data , stores in offline sqlite database later starts syncing whenever internet connection available.
from sqllite database i'm syncing entry 1 one status "pending"
i made working codes these not efficient , i'm facing these problems.
1 - broadcastreceiver activates when network status changes doesn't work when phone connected internet. want start syncing data whenever internet available not after chaning internet status
2 - i'm using simple loop starts first index of row status "pending" , runs till reaches total number of pending items. want efficient way that.
i used 2 functions inside databasehandler class,
function #1 pendingcontactcounter()
counts number of contacts status "pending". example if consider uploaded table image function return 2 two contact availble status "pending"
function #2 pendingcontactid()
returns id of first row status "pending" above image return 2 id 2 has status "pending"
for internet connection detection made broadcastreceiver
public class internetreceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { connectivitymanager cm = ((connectivitymanager) context .getsystemservice(context.connectivity_service)); if (cm == null) return; if (cm.getactivenetworkinfo() != null && cm.getactivenetworkinfo().isconnected()) { // connected log.d("connection status", "connected!"); intent service_intent = new intent(context, uploadservice.class); context.startservice(service_intent); } } else { // not connected log.d("connection status", "not connected!"); } }
and service updates "pending" entries "sent"
public class uploadservice extends intentservice { private int total_request; private int pending_id; public uploadservice() { super("uploadservice"); } @override protected void onhandleintent(intent intent) { databasehandler db = new databasehandler(getapplicationcontext()); total_request = db.pendingcontactcounter(); pending_id = db.pendingcontactid(); log.d("total request: ", "" + total_request); log.d("pending id: ", "" + pending_id); (int = pending_id; <= total_request + 1; i++) { db.updatedata(i, "sent"); } } }
finally manifest looks this,
<receiver android:name=".connectionreceiver" > <intent-filter> <action android:name="android.intent.action.boot_completed" /> <action android:name="android.net.wifi.state_change" /> <action android:name="android.net.conn.connectivity_change" /> </intent-filter> </receiver> <service android:name="uploadservice" > </service>
Comments
Post a Comment