【问题标题】:Android: how to get the intent received by a service?Android:如何获取服务接收到的意图?
【发布时间】:2010-12-30 05:09:10
【问题描述】:

我正在启动一项服务,目的是在其中添加额外信息。

如何在我的服务代码中获取意图?

在服务中没有像 getIntent().getExtras() 这样的函数。

【问题讨论】:

    标签: android service android-intent


    【解决方案1】:

    onStart() 现已弃用。你应该改用onStartCommand(Intent, int, int)

    【讨论】:

      【解决方案2】:

      覆盖onStart() -- 您收到Intent 作为参数。

      【讨论】:

      • 能否详细解释一下
      【解决方案3】:

      传递额外内容:

      Intent intent = new Intent(this, MyService.class);
      intent.putExtra(MyService.NAME, name);
      ...
      startService(intent);
      

      要检索服务中的附加功能:

      public class MyService extends Service {  
          @Override
          public int onStartCommand(Intent intent, int flags, int startId) {
              super.onStartCommand(intent, flags, startId);
              String name = intent.getExtras().getString(NAME);
              ...
          } 
          ...
      } 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-05
        • 1970-01-01
        相关资源
        最近更新 更多