在开始的onClick服务onClick

2023-09-04 10:39:26 作者:先森、你爱我吗?~

我要当用户单击一个按钮来启动服务。

I want to start a service when the user clicks a button.

基本上当用户点击开始按钮,在服务应该开始记录GPS cordinates,当他点击停止则服务应该终止​​。

Basically When the user clicks the start button the service should start recording gps cordinates and when he clicks stop then the service should terminate.

我应该如何去实现这一点。

How should i go about implementing this.

推荐答案

我不明白为什么要为了启动一个服务启动/停止记录GPS坐标。所以,我给你两个答案。一会告诉你如何启动和停止按钮的服务,和其他将告诉你如何启动/停止记录GPS坐标这并不需要与服务做了(虽然可以改变这样做)。

I'm not quite sure why you want to start a service in order to start/stop recording gps coordinates. So I'll give you two answers. One will show you how to start and stop a service with buttons and the other will show you how to start/stop recording gps coordinates which does not need to be done with a service (though can be changed to do so).

开始/停止服务的按钮

主要的事情,你需要做的就是加入安卓的onClick =functionToCall到按钮的XML标记。替换 functionToCall 与实际功能的名称。然后,你必须作出这样的函数调用无论是 startService() stopService()函数来启动/停止服务。这是我的启动示例程序/停止一个调用SayHello的服务。

The main thing you have to do is add android:onClick="functionToCall" to the button xml tag. Replace functionToCall with the real function name. Then you have to make that function call either the startService() or stopService() function to start/stop the service. Here is my example program that starts/stops a service that called SayHello.

您可以忽略大多数遵循XML只是注意到安卓的onClick =

You can ignore most of the following xml just notice the android:onClick=""

main.xml中:

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(我做的活动持有的按钮):

ServiceClick.java (the activity I made that holds the buttons):

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的服务,所以一定要改变意向,呼吁你想要的服务。

I'm sure you don't want to start/stop the SayHello Service, so make sure you change Intent to call for the service you do want.