将感测数据从位置传感器发送到服务器的服务

时间:2014-07-01 15:35:23

标签: android android-service android-sensors

我是android新手。我需要一个服务(在一个单独的线程中),它定期将感应数据从位置传感器发送到服务器。任何人都可以用一个例子来解释。我的意思是服务的一部分,如何在一个单独的线程中实现它,在那里放置代码以将数据发送到服务器。我已经实现了从传感器获取数据的代码。

提前致谢!

1 个答案:

答案 0 :(得分:0)

您必须使用startForeground

实现Service class

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

并阅读此主题:Implement startForeground method in Android

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