服务,它从位置传感器向服务器发送感测数据



我是android新手。我需要一个服务(在一个单独的线程中),它定期从位置传感器发送感测数据到服务器。谁能举个例子解释一下?我指的只是服务的一部分,如何在一个单独的线程中实现它,在哪里放置代码来发送数据到服务器。我已经实现了从传感器获取数据的代码。

提前感谢!

您必须通过startForeground实现它来使用Service类

阅读本文:http://android-developers.blogspot.de/2010/02/service-api-changes-starting-with.html

在Android 中实现startForeground方法
 final static int myID = 1234;
//The intent to launch when the user clicks the expanded notification
Intent intent = new Intent(this, SomeActivityToLaunch.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, 0);
//This constructor is deprecated. Use Notification.Builder instead
Notification notice = new Notification(R.drawable.icon_image, "Ticker text", System.currentTimeMillis());
//This method is deprecated. Use Notification.Builder instead.
notice.setLatestEventInfo(this, "Title text", "Content text", pendIntent);
notice.flags |= Notification.FLAG_NO_CLEAR;
startForeground(myID, notice);

最新更新