【发布时间】:2011-09-19 05:29:32
【问题描述】:
这是我将值从活动类传递给我的服务类的代码。
Intent i = new Intent(this, MyAlarmService.class);
i.putExtra("rowId", rowId);
startactivity(i);
如何在我的服务类中获取该 rowId ? 请帮帮我
【问题讨论】:
标签: android service android-intent
这是我将值从活动类传递给我的服务类的代码。
Intent i = new Intent(this, MyAlarmService.class);
i.putExtra("rowId", rowId);
startactivity(i);
如何在我的服务类中获取该 rowId ? 请帮帮我
【问题讨论】:
标签: android service android-intent
试试这个,
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");
}
【讨论】:
您在活动中添加了额外的内容
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;
}
【讨论】:
尝试:
在MyAlarmService.class
在onStartCommand方法中写入。
String rid = intent.getStringExtra("rowId", rowId);
你会得到答案的。
【讨论】: