这是我的代码,将活动类的值传递给我的服务类。如何获取从活动类传递的服务类中的值?
Intent i = new Intent(this, MyAlarmService.class);
i.putExtra("rowId", rowId);
startactivity(i);
我该如何在我的服务类中获取rowId? 请帮我
这是我的代码,将活动类的值传递给我的服务类。如何获取从活动类传递的服务类中的值?
Intent i = new Intent(this, MyAlarmService.class);
i.putExtra("rowId", rowId);
startactivity(i);
我该如何在我的服务类中获取rowId? 请帮我
试试这个,
public int onStartCommand (Intent intent, int flags, int startId)
{
super.onStartCommand(intent, flags, startId);
String id = intent.getStringExtra("rowId");
}
为此,您可以
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");
}
尝试:
在中onStartCommand
方法MyAlarmService.class
写。
String rid = intent.getStringExtra("rowId", rowId);
你会得到答案。
您在活动投入额外
Intent intent = new Intent(this, Service.class);
intent.putExtra("Key", "String");
你得到StringExtra在服务类
String ba;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
ba = intent.getStringExtra("Key");
return START_STICKY;
}
可能http://stackoverflow.com/questions/1964969/android-how-to的副本 - 获得最意向收到按即服务 – 2011-09-19 05:32:16