如何启动仅服务的 Android 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/990217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:33:24  来源:igfitidea点击:

How to start Service-only Android app

androidservice

提问by Anurag Uniyal

I am creating an application whose only component is a servicewhich keeps on running in background (basically a proxy server) but I am not able to find a way how to start that service. Application can not have any UI or user interaction so I am not using Activity.
Broadcast receivercan listen to BOOT broadcast but how do I start service first time when it is installed and how can I keep it running? or is there a broadcast which I can listen after app is installed e.g. may be TIME_TICK but that has to be registered from activity I think.

我正在创建一个应用程序,它的唯一组件是service在后台继续运行(基本上是代理服务器),但我无法找到如何启动该服务的方法。应用程序不能有任何 UI 或用户交互,所以我没有使用 Activity。
Broadcast receiver可以收听 BOOT 广播,但如何在安装后第一次启动服务以及如何使其保持运行?或者是否有我可以在安装应用程序后收听的广播,例如可能是 TIME_TICK 但我认为必须从活动中注册。

采纳答案by snctln

Unfortunately right now there is no reliable way to receive a broadcast event after your applicaiton has been installed, the ACTION_PACKAGE_ADDEDIntent does not broadcast to the newly installed package.

不幸的是,现在没有可靠的方法在您的应用程序安装后接收广播事件,ACTION_PACKAGE_ADDEDIntent 不会广播到新安装的包。

You will have to have a broadcast receiver class as well as your service in order to receive the ACTION_BOOT_COMPLETEDevent. I would also recommend adding the ACTION_USER_PRESENTintent to be caught by that broadcast receiver, this requires Android 1.5 (minSDK=3), this will call your broadcast receiver whenever the user unlocks their phone. The last thing that you can do to try to keep your service running without having it easily be shut down automatically is to call Service.setForeground()in your service onCreate to tell Android that your service shouldn't be stopped, this was added mainly for mp3 player type services that have to keep running but can be used by any service.

为了接收ACTION_BOOT_COMPLETED事件,您必须有一个广播接收器类以及您的服务。我还建议添加由该广播接收器捕获的ACTION_USER_PRESENT意图,这需要 Android 1.5 (minSDK=3),这将在用户解锁手机时调用您的广播接收器。您可以做的最后一件事是在您的服务 onCreate 中调用Service.setForeground()来告诉 Android 您的服务不应停止,这主要是添加的用于必须保持运行但可由任何服务使用的 mp3 播放器类型服务。

Make sure you add the proper permissions for the boot_complete and user_present events in you manifest.

确保为清单中的 boot_complete 和 user_present 事件添加适当的权限。

Here is a simple class that you can use as a broadcast receiver for the events.

这是一个简单的类,您可以将其用作事件的广播接收器。

package com.snctln.util.WeatherStatus;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class WeatherStatusServiceReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        if(intent.getAction() != null)
        {
            if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED) ||
                intent.getAction().equals(Intent.ACTION_USER_PRESENT))
            {
                context.startService(new Intent(context, WeatherStatusService.class));
            }
        }
    }
};

Good luck.

祝你好运。

回答by shaobin0604

Return START_STICKYin Service.onStartCommand

返回START_STICKYService.onStartCommand

Android will restart your service

Android 将重新启动您的服务

回答by jboi

I'm researching on the same issue. Here's is what I've found: http://kfb-android.blogspot.de/2009/04/registering-for-timetick-after-reboot.html

我正在研究同样的问题。这是我发现的:http: //kfb-android.blogspot.de/2009/04/registering-for-timetick-after-reboot.html

That simple little demo shows a solution by playing a little bit of ping-pong between a BroadcatReceiverand a Service. The Receiver is registered to start at boot time by registering in the Manifest to receive ACTION_BOOT_COMPLETED. It just starts a service when receviving ACTION_BOOT_COMPLETED. The Servicein turn registeres the BroadcastReceiverthen for the ACTION_TIME_TICK.

这个简单的小演示通过在 aBroadcatReceiver和 a之间打一点乒乓来展示一个解决方案Service。通过在清单中注册以接收ACTION_BOOT_COMPLETED. 它只是在 receviving 时启动服务ACTION_BOOT_COMPLETED。该Service反过来registeres在BroadcastReceiver随后的ACTION_TIME_TICK

The app logic will be implemented in the BroadcastReceiver.

应用程序逻辑将在BroadcastReceiver.