在 onClick 上启动服务 [英] Start a service on onClick

查看:18
本文介绍了在 onClick 上启动服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

Basically, when the user clicks the start button the service should start recording GPS coordinates and when he clicks stop the service should terminate.

我应该如何实施这个?

推荐答案

我不太确定您为什么要启动服务以开始/停止记录 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).

使用按钮启动/停止服务

您需要做的主要事情是将 android: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,只需注意 android:onClick=""

You can ignore most of the following xml just notice the 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(我制作的保存按钮的活动):

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 服务,因此请确保更改 Intent 以调用您想要的服务.

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.

这篇关于在 onClick 上启动服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆