【问题标题】:service which sends the sensed data from location sensor to the server将传感数据从位置传感器发送到服务器的服务
【发布时间】:2014-07-01 15:35:23
【问题描述】:
我是安卓新手。我需要一个服务(在一个单独的线程中),它会定期将感测到的数据从位置传感器发送到服务器。任何人都可以用一个例子来解释。我的意思只是服务的一部分,如何在一个单独的线程中实现它来放置代码以将数据发送到服务器。我已经实现了从传感器获取数据的代码。
提前致谢!
【问题讨论】:
标签:
android
android-service
android-sensors
【解决方案1】:
您必须通过使用 startForeground 来实现 Service 类
阅读这篇文章: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);