回答
我不確定你爲什麼要開始一項服務,以開始/停止記錄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服務器上自動啓動weinre服務器
- 2. 在nodejitsu上啓動redis服務器
- 3. 無法在Ubuntu上啓動Jenkins服務
- 4. 在CentOS上啓動selenium服務器
- 5. 在android平臺上啓動服務
- 6. Memcached服務無法啓動在Windows上
- 7. 在線程上啓動多個服務
- 8. npm無法在服務器上啓動
- 9. red5服務無法在Windows上啓動
- 10. 如何在linux上啓動bonjour服務
- 11. 如何在服務器上啓動PlayFramework
- 12. Glassfish服務器不在eclipse上啓動
- 13. PHP在服務器上啓動PowerPoint
- 14. MySQL服務無法在XAMPP上啓動
- 15. pyVmomi在虛擬機上啓動服務
- 16. 在Digital Ocean上啓動Node.js服務器
- 17. 在LINUX上啓動Tomcat作爲服務
- 18. 在windows上啓動neo4j服務器
- 19. 在啓動時在Raspberry PI上啓動瘦服務器
- 20. 如何在啓動時在OSX上啓動Jenkins服務器
- 21. 從服務啓動服務
- 22. 從服務啓動服務
- 23. 啓動服務
- 24. 啓動服務
- 25. 在啓動時啓動Kivy服務(Android)
- 26. 在活動開啓時啓動服務
- 27. Android - 在啓動時啓動服務
- 28. 在啓動時啓動gpsd服務
- 29. 防止在啓動時啓動服務
- 30. boot_completed上的自動啓動服務
[如何處理按鈕點擊](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