回答
我不确定你为什么要开始一项服务,以开始/停止记录GPS坐标。所以我会给你两个答案。一个会告诉你如何启动和停止按钮的服务,另一个将告诉你如何开始/停止记录GPS服务的坐标,而不需要使用服务来完成(虽然可以改变这样做)。
启动/停止服务的按钮
你必须做的主要事情是添加android:onClick="functionToCall"
按钮XML标签。将functionToCall
替换为真实的函数名称。然后,必须使该功能调用startService()
或stopService()
函数来启动/停止该服务。这是我的示例程序,用于启动/停止一个名为SayHello的服务。
可以忽略大多数遵循XML只是注意到android:onClick=""
的main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:text="Start"
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="startClicked">
</Button>
<Button android:text="Stop"
android:id="@+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="stopClicked">
</Button>
</LinearLayout>
ServiceClick.java(我做了保存按钮活动):
package com.ServiceClick;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ServiceClick extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void startClicked(View view) {
startService(new Intent("SayHello"));
}
public void stopClicked(View view) {
stopService(new Intent("SayHello"));
}
}
我确定你不想开始/停止SayHello服务,所以请确保你改变了意图来呼叫你想要的服务。
我决定把GPS定位录音的答案放到一个新帖子中,让事情变得更加清洁。
记录GPS坐标
我们需要做的第一件事是添加一行到我们的AndroidManifest.xml中说,我们希望允许记录GPS坐标:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" .... >
....
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>
(我把在那些....
表示我省略了一些内容)
接下来,您必须将android:onClick="functionToCall"
添加到每个按钮标记(有关更多信息,请参阅我的其他答案)细节)。按钮标签应该是这个样子:
<Button android:text="Start"
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="startButton"></Button>
<Button android:text="Stop"
android:id="@+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="stopButton"></Button>
现在你要问的系统为LocationManager
,我们可以给当位置收到了LocationListener
使用。当开始按钮被击中时,我们将给出LocationManager
LocationListener
,并在停止按钮被击中时移除该监听器。 LocationListener
将调用一个函数来存储位置。
下面是代码这样做:
package com.TrackLocation;
import java.util.ArrayList;
//Ommitted rest of the imports
public class TrackLocation extends Activity {
ArrayList<Location> recordedLocations = new ArrayList<Location>();
LocationManager locationManager;
LocationListener locationListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Get the manager from the system
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Create the locationListener that we will be adding and removing
locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
recordLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
}
public void recordLocation(Location loc) {
recordedLocations.add(loc);
}
public void startButton(View view) {
//Add the listener asking for GPS
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
public void stopButton(View view) {
locationManager.removeUpdates(locationListener);
}
}
上面的代码并没有做太多的价值观(事实上,你甚至看不到的位置,而不使用调试器),但我想尽可能保持这一点。我有更完整的代码版本,它将显示ListView
中的位置。这是更完整版本的link。
- 1. 在LINUX上启动Tomcat作为服务
- 2. 在nodejitsu上启动redis服务器
- 3. pyVmomi在虚拟机上启动服务
- 4. 在Digital Ocean上启动Node.js服务器
- 5. npm无法在服务器上启动
- 6. 在android平台上启动服务
- 7. Memcached服务无法启动在Windows上
- 8. 在windows上启动neo4j服务器
- 9. PHP在服务器上启动PowerPoint
- 10. red5服务无法在Windows上启动
- 11. 无法在Ubuntu上启动Jenkins服务
- 12. 如何在启动时在OSX上启动Jenkins服务器
- 13. 启动服务
- 14. 启动服务
- 15. Android - 在启动时启动服务
- 16. 在启动时启动Kivy服务(Android)
- 17. 在活动开启时启动服务
- 18. boot_completed上的自动启动服务
- 19. 在服务器上启动后弹簧启动停止
- 20. 在CentOS上启动时启动Rails服务器
- 21. Tomcat 6.0.18服务无法在Windows服务器上启动
- 22. 在Win 2008服务器上停止/启动Uniface服务
- 23. WCF服务无法在Web服务器上启动调试
- 24. 如何在Spigot服务器上启动外部Netty服务器
- 25. 上重新启动服务器proftp的服务不会自动
- 26. 在Android中启动服务
- 27. Windows服务在Windows 7上启动,但无法在Windows Server 2008 R2上启动
- 28. 启动Windows服务
- 29. wcf服务启动
- 30. Android服务启动
[如何处理按钮点击](http://developer.android.com/reference/android/widget/Button.html)[如何启动一个'服务'](http://developer.android。 COM /参考/机器人/内容/ Context.html#startService%28android.content.Intent%29) – tbruyelle 2011-03-23 21:06:14