【问题标题】:getIntent() method is undefined inside of onStartCommand() method of android servicegetIntent() 方法在 android 服务的 onStartCommand() 方法中未定义
【发布时间】:2012-10-16 08:17:00
【问题描述】:

有谁知道为什么我的 getIntent() 方法在 android 服务和 onStartCommand() 方法内部未定义?

   @Override
public int onStartCommand(Intent intent, int flags, int startId){
super.onStartCommand(intent, startId, startId);

Intent i = getIntent();
Bundle extras = i.getExtras();
filename = extras.getString("filename");


 return START_STICKY;
}

【问题讨论】:

    标签: android service android-intent undefined onstart


    【解决方案1】:

    因为intent是作为函数的参数传递的……

    onStartCommand(Intent intent, ...
        Bundle extras = intent.getExtras();
    

    【讨论】:

      【解决方案2】:

      getIntent() 方法属于活动而非服务。在Activity中如果要获取当前的Intent,可以调用getIntent()方法获取Intent。在服务中,onStartCommand() 方法已经具有参数意图,因此您可以获取与意图一起传递的额外数据。

      您已经从onStartCommand() 方法获得intent 对象。检查一下。

      【讨论】:

        【解决方案3】:

        Service 类中没有 getIntent() 方法。您可以使用传递给 onStartCommand

        的意图参数

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-01-02
          • 2012-08-10
          • 1970-01-01
          • 1970-01-01
          • 2011-08-27
          • 2019-04-29
          • 2012-05-03
          • 2015-04-26
          相关资源
          最近更新 更多