Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android後台上傳數據demo

android後台上傳數據demo

編輯:關於Android編程

1、界面啟動後開啟服務

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、後台服務組件負責上傳數據

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


// 定期的上傳用戶的數據.
timer = new Timer();
final Random random = new Random();
task = new TimerTask() {
@Override
public void run() {


if (km.inKeyguardRestrictedInputMode()) {//鎖屏狀態下
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();
}


}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved