【问题标题】:Start a Service from an Activity从 Activity 启动服务
【发布时间】:2011-01-21 01:17:58
【问题描述】:

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

【问题讨论】:

    标签: android android-activity android-service


    【解决方案1】:

    在你的代码中添加这个

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

    别忘了在 AndroidManifest.xml 文件中添加服务标签

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

    来自Android official documentation

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        首先从 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));
        }
        

        【讨论】:

          【解决方案4】:

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

          【讨论】:

            【解决方案5】:

            使用 Context.startService() 方法。

            并阅读this

            【讨论】:

              【解决方案6】:

              kotlin 中,您可以从以下活动开始服务:

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

              【讨论】:

                【解决方案7】:

                如果您想启动一个服务并且它应该在后台运行,请在您的相应服务中使用 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 Receiver 中添加,

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

                你的服务就像,

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2011-05-09
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2017-11-23
                  • 1970-01-01
                  相关资源
                  最近更新 更多