我需要将价值传递给服务阶层

时间:2018-09-12 13:26:59

标签: android android-service

我需要将值传递给服务类。

这就是我从活动中致电服务的方式:

caffe model

这是我的服务:

try {
  Intent lintent = new Intent(getApplicationContext(), BackgroundService.class);
  getApplicationContext().stopService(lintent);

  startService (new Intent(this, BackgroundService.class));
}catch (Exception s){}

我正在努力将价值传递给服务阶层,任何志愿者都可以帮助我解决这个问题

1 个答案:

答案 0 :(得分:0)

您可以使用Intent传递参数:

Intent serviceIntent = new Intent(this,ListenLocationService.class); 
   serviceIntent.putExtra("ID", "1234");
   startService(serviceIntent);

并在服务类的onStart方法中获取参数

@Override
public void onStart(Intent intent, int startId) {
  super.onStart(intent, startId);
  Bundle extras = intent.getExtras(); 
  if(extras != null) {
    String stringID = (String) extras.get("ID");
  }
}