在没有主 Activity 的情况下启动 Android 应用程序并在启动应用程序时启动服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10909683/
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 05:25:09  来源:igfitidea点击:

Launch Android application without main Activity and start Service on launching application

androidandroid-activityandroid-service

提问by Meher

I have the following scenario in my application. There is no UI in my application; instead there is a Service which starts on boot up and will continuously run.

我的应用程序中有以下场景。我的应用程序中没有 UI;相反,有一个服务在启动时启动并持续运行。

How can I configure my manifest file without a main Activity? Can I launch my app without any Activity? And on launching my app, my Service should start. Is this possible?

如何在没有主活动的情况下配置我的清单文件?我可以在没有任何活动的情况下启动我的应用程序吗?在启动我的应用程序时,我的服务应该启动。这可能吗?

I don't want to make a translucent Activity to start the Service.

我不想做一个半透明的活动来启动服务。

回答by Darshan Rivka Whittle

You said you didn't want to use a translucent Activity, but that seems to be the best way to do this:

你说你不想使用半透明的活动,但这似乎是最好的方法:

  1. In your Manifest, set the Activity theme to Theme.Translucent.NoTitleBar.
  2. Don't bother with a layout for your Activity, and don't call setContentView().
  3. In your Activity's onCreate(), start your Service with startService().
  4. Exit the Activity with finish()once you've started the Service.
  1. 在您的清单中,将活动主题设置为Theme.Translucent.NoTitleBar.
  2. 不要为您的 Activity 的布局而烦恼,也不要调用setContentView().
  3. 在您的 Activity 中onCreate(),使用startService().
  4. finish()启动服务后退出活动。

In other words, your Activity doesn't have to be visible; it can simply make sure your Service is running and then exit, which sounds like what you want.

换句话说,您的 Activity 不必是可见的;它可以简单地确保您的服务正在运行,然后退出,这听起来像您想要的。

I would highly recommend showing at least a Toast notificationindicating to the user that you are launching the Service, or that it is already running. It is very bad user experience to have a launcher icon that appears to do nothing when you press it.

我强烈建议至少显示一个Toast 通知,向用户表明您正在启动该服务,或者它已经在运行。当您按下启动器图标时,它似乎什么也不做,这是非常糟糕的用户体验。

回答by Lalit Poptani

Yes you can do that by just creating a BroadcastReceiverthat calls your Servicewhen your Application boots. Here is a complete answer given by me.
Android - Start service on boot

是的,您可以通过创建一个在您的应用程序启动时BroadcastReceiver调用您的方法来做到这一点Service。这是我给出的完整答案。
Android - 启动时启动服务

If you don't want any icon/launcher for you Application you can do that also, just don't create any Activity with

如果您不想要任何图标/启动器应用程序,您也可以这样做,只是不要创建任何活动

<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

Just declare your Serviceas declared normally.

只需声明您的Service正常声明即可。

回答by Mahdi-Malv

The reason to make an App with no activity or service can be making a Widget app that doesn't need to be started.
Once you start a project don't create any activities. After you created the project just hit run. Android studio will say No default activity found.

Click Edit Configuration(From the Runmenu) and in the Launch optionpart set the Launchvalue to Nothing. Then click ok and run the App. No app will be show in the Apps menu.

制作没有活动或服务的应用程序的原因可能是制作不需要启动的小部件应用程序。
一旦你开始一个项目,不要创建任何活动。创建项目后,只需点击运行即可。Android 工作室会说No default activity found

单击Edit Configuration(从Run菜单)并在Launch option部分将Launch值设置为Nothing。然后单击确定并运行该应用程序。应用程序菜单中不会显示任何应用程序

回答by Merc

Android Studio Version 2.3

安卓工作室 2.3 版

You can create a Service without a Main Activity by following a few easy steps. You'll be able to install this app through Android Studio and debug it like a normal app.

您可以按照几个简单的步骤创建没有主活动的服务。您将能够通过 Android Studio 安装此应用程序并像普通应用程序一样调试它。

First, create a project in Android Studio without an activity. Then create your Service class and add the service to your AndroidManifest.xml

首先,在没有活动的情况下在 Android Studio 中创建一个项目。然后创建您的服务类并将服务添加到您的 AndroidManifest.xml

<application android:allowBackup="true"
    android:label="@string/app_name"
    android:icon="@mipmap/ic_launcher"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <service android:name="com.whatever.myservice.MyService">
        <intent-filter>
            <action android:name="com.whatever.myservice.MyService" />
        </intent-filter>
    </service>
</application>

Now, in the drop down next to the "Run" button(green arrow), go to "edit configurations" and within the "Launch Options" choose "Nothing". This will allow you to install your Service without Android Studio complaining about not having a Main Activity.

现在,在“运行”按钮(绿色箭头)旁边的下拉菜单中,转到“编辑配置”并在“启动选项”中选择“无”。这将允许您安装您的服务,而不会让 Android Studio 抱怨没有主活动。

Once installed, the service will NOT be running but you will be able to start it with this adb shell command...

安装后,该服务将不会运行,但您可以使用此 adb shell 命令启动它...

am startservice -n com.whatever.myservice/.MyService

Can check it's running with...

可以检查它正在运行...

ps | grep whatever

I haven't tried yet but you can likely have Android Studio automatically start the service too. This would be done in that "Edit Configurations" menu.

我还没有尝试过,但您也可以让 Android Studio 自动启动该服务。这将在“编辑配置”菜单中完成。