【问题标题】:How to get the value in my service class which is passed from activity class?如何获取从活动类传递的服务类中的值?
【发布时间】:2011-09-19 05:29:32
【问题描述】:

这是我将值从活动类传递给我的服务类的代码。

Intent i =  new Intent(this, MyAlarmService.class);
i.putExtra("rowId", rowId);
startactivity(i);

如何在我的服务类中获取该 rowId ? 请帮帮我

【问题讨论】:

标签: android service android-intent


【解决方案1】:

试试这个,

public int onStartCommand (Intent intent, int flags, int startId)
{
     super.onStartCommand(intent, flags, startId);
     String id = intent.getStringExtra("rowId");
}

【讨论】:

    【解决方案2】:

    为此你可以

    Override onStart() -- you receive the Intent as a parameter,
    

    onStart() is deprecated now. onStartCommand(Intent, int, int) should be used instead,
    

    那么,

    String id = intent.getStringExtra("rowId");
    

    Bundle extras = getIntent().getExtras(); 
    String rowId;
    if (extras != null) {
        rowId = extras.getString("rowId");          
    }
    

    【讨论】:

      【解决方案3】:

      您在活动中添加了额外的内容

      Intent intent = new Intent(this, Service.class); 
      intent.putExtra("Key", "String");
      

      你在 Service 类中得到 StringExtra

      String ba;    
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
          ba = intent.getStringExtra("Key");
          return START_STICKY;
      }
      

      【讨论】:

        【解决方案4】:

        尝试:

        MyAlarmService.class

        onStartCommand方法中写入。

        String rid = intent.getStringExtra("rowId", rowId);
        

        你会得到答案的。

        【讨论】:

          猜你喜欢
          • 2011-11-14
          • 2015-03-11
          • 1970-01-01
          • 1970-01-01
          • 2021-07-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多