Android 从活动启动服务

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

Start a Service from activity

android

提问by MAkS

In my app i have an activity from which i want to start an Service Can any body help me?

在我的应用程序中,我有一项活动,我想从中启动一项服务 任何机构都可以帮助我吗?

回答by Vihaan Verma

Add this in your code

将此添加到您的代码中

Intent serviceIntent = new Intent(this, ServiceName.class);
    startService(serviceIntent);

Dont forget to add service tag in AndroidManifest.xml file

不要忘记在 AndroidManifest.xml 文件中添加服务标签

<service android:name="com.example.ServiceName"></service>

From the Android official documentation:

来自Android官方文档

Caution: A service runs in the same process as the application in which it is declared and in the main thread of that application, by default. So, if your service performs intensive or blocking operations while the user interacts with an activity from the same application, the service will slow down activity performance. To avoid impacting application performance, you should start a new thread inside the service.

注意:默认情况下,服务在与声明它的应用程序相同的进程中以及该应用程序的主线程中运行。因此,如果您的服务在用户与来自同一应用程序的 Activity 交互时执行密集或阻塞操作,则该服务会降低 Activity 的性能。为避免影响应用程序性能,您应该在服务内启动一个新线程。

回答by Pierre-Antoine LaFayette

The application can start the service with the help of the Context.startService method. The method will call the onCreate method of the service if service is not already created; else onStart method will be called. Here is the code:

应用程序可以在Context.startService 方法的帮助下启动服务。如果服务尚未创建,该方法将调用服务的 onCreate 方法;否则将调用 onStart 方法。这是代码:

Intent serviceIntent = new Intent();
serviceIntent.setAction("com.testApp.service.MY_SERVICE");
startService(serviceIntent);

回答by Goutam

First create service from android Manifest.xmlfile (i.e from application tab) and give some name to it.
On the activity on some event like click or touch to include the code from the service:

首先从 androidManifest.xml文件(即从应用程序选项卡)创建服务并为其命名。
在某些事件的活动中,例如单击或触摸以包含来自服务的代码:

public void onClick(View v)
{
    startService(new Intent(getApplicationContext(),Servicename.class));
}

If you want to stop the running or started service then include this code:

如果要停止正在运行或已启动的服务,请包含以下代码:

public void onclick(View v)
{
    stopService(new Intent(getApplicationContext,Servicename.class));
}

回答by Josh Lee

The API Demoshave some examples that launch services.

API演示有一些例子,发射服务。

回答by Vlad Tsepelev

Use a Context.startService() method.

使用 Context.startService() 方法。

And read this.

并阅读此内容

回答by Ajith K P

If you want to start a service and it should run in background use START_STICKY in your corresponding service.

如果您想启动一个服务并且它应该在后台运行,请在您的相应服务中使用 START_STICKY。

You can start servvice with on boot also,

您也可以在启动时启动服务,

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

And create receiver,

并创建接收器,

<receiver android:name=".auth.NotificationBroadcast" android:enabled="true" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

In Brodcast Receiver add,

在广播接收器中添加,

@Override
    public void onReceive(Context context, Intent intent) {

        System.out.println("BroadcastReceiverBroadcast--------------------ReceiverBroadcastReceiverBroadcastReceiver----------------BroadcastReceiver");

        if (intent != null) {
            String action = intent.getAction();

        switch (action) {
            case Intent.ACTION_BOOT_COMPLETED:
                System.out.println("Called on REBOOT");
                // start a new service 
               startService(new Intent(getApplicationContext(),Servicename.class));

                break;
            default:
                break;
        }
    }
}

And your service is like,

你的服务就像,

回答by Sanam Yavarpor

in kotlin you can start service from activity as follow :

在 kotlin 中,您可以从活动中启动服务,如下所示:

   startService!!.setOnClickListener { startService() }

   private fun startService(){
    startService(Intent(this, HitroService::class.java))
   }