在onClick上启动服务

时间:2011-03-23 20:54:45

标签: android button service gps

我想在用户点击按钮时启动服务。

基本上,当用户点击开始按钮时,服务应该开始记录GPS坐标,当他点击停止时,服务应该终止。

我该如何实施呢?

2 个答案:

答案 0 :(得分: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服务,因此请确保更改Intent以调用您想要的服务。

答案 1 :(得分: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。当按下开始按钮时,我们将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

上面的代码对值没有太大作用(实际上你甚至不能在不使用调试器的情况下看到位置),但我希望尽可能地保持这个。我有更全面的代码版本,可以在{{1}}中显示位置。这是更完整版本的link