Android Background Upload data demo

Source: Internet
Author: User

1. Open the service after the interface starts

public class Uploadlogactivity extends Activity {
/** called when the activity is first created. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Intent Intent = new Intent (this,uploadservice.class);
StartService (Intent);
}

2. Background service component is responsible for uploading data

public class Uploadservice extends Service {
private timer timer;
Private TimerTask task;
Private Keyguardmanager km;


@Override
Public IBinder Onbind (Intent Intent) {
TODO auto-generated Method Stub
return null;
}


@Override
public void OnCreate () {
km = (keyguardmanager) getsystemservice (Keyguard_service);


Upload the user's data on a regular basis.
Timer = new timer ();
Final random random = new random ();
task = new TimerTask () {
@Override
public void Run () {


if (Km.inkeyguardrestrictedinputmode ()) {//Lock screen State
Intent Intent = new Intent ();
Intent.setaction (Intent.action_view);
Intent.addcategory (intent.category_browsable);
Intent.setflags (Intent.flag_activity_new_task);
Intent.setdata (Uri
. Parse ("http://192.168.1.2:8080/web/aaa?info="
+ Random.nextint ()));
StartActivity (Intent);
}
}
};


Timer.schedule (Task, 1000, 2000);
Super.oncreate ();
}


}

Android Background Upload data demo

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.