2011-03-23 53 views
2

我想在用戶單擊按鈕時啓動服務。在onClick上啓動服務

基本上,當用戶點擊開始按鈕時,服務應該開始記錄GPS座標,當他點擊停止服務應該終止。

我應該如何去實現這個?

+0

[如何處理按鈕點擊](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

回答

3

我不確定你爲什麼要開始一項服務,以開始/停止記錄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服務,所以請確保你改變了意圖來呼叫你想要的服務。

1

我決定把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使用。當開始按鈕被擊中時,我們將給出LocationManagerLocationListener,並在停止按鈕被擊中時移除該監聽器。 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