2011-03-23 83 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