从 Activity 启动服务

发布于 2024-08-23 03:21:11 字数 41 浏览 2 评论 0原文

在我的应用程序中,我有一个活动,我想从中启动服务。有人可以帮助我吗?

In my app, I have an Activity from which I want to start a Service. Can anybody help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

霓裳挽歌倾城醉 2024-08-30 03:21:11

在您的代码中添加此内容

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

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

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

来自 Android 官方文档

警告:服务与应用程序在同一进程中运行
它是在该应用程序的主线程中声明的
默认。因此,如果您的服务执行密集或阻塞操作
当用户与同一应用程序中的活动进行交互时,
该服务会降低活动性能。为避免影响
应用程序性能,你应该在内部启动一个新线程
服务。

Add this in your code

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

Dont forget to add service tag in AndroidManifest.xml file

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

From the Android official documentation:

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.

維他命╮ 2024-08-30 03:21:11

应用程序可以借助 Context.startService 方法启动服务。如果service还没有创建,该方法会调用service的onCreate方法;否则 onStart 方法将被调用。这是代码:

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

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:

Intent serviceIntent = new Intent();
serviceIntent.setAction("com.testApp.service.MY_SERVICE");
startService(serviceIntent);
野侃 2024-08-30 03:21:11

首先从 android Manifest.xml 文件(即从应用程序选项卡)创建服务并为其指定一些名称。
在某些事件(例如单击或触摸)的活动上包含来自服务的代码:

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

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

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

First create service from android Manifest.xml file (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:

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));
}
云之铃。 2024-08-30 03:21:11

API 演示 有一些启动服务的示例。

The API Demos have some examples that launch services.

心病无药医 2024-08-30 03:21:11

使用 Context.startService() 方法。

并阅读本文

Use a Context.startService() method.

And read this.

等往事风中吹 2024-08-30 03:21:11

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

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

in kotlin you can start service from activity as follow :

   startService!!.setOnClickListener { startService() }
 
   private fun startService(){
    startService(Intent(this, HitroService::class.java))
   }
自由如风 2024-08-30 03:21:11

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

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

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

并创建接收器,

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

在 Brodcast 接收器中添加,

@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;
        }
    }
}

您的服务就像,

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

You can start service 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,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文