将 intent extra 的子字符串传递给服务时正在更改 class
Substring of the intent extra is being changed when passing it to Service class
我正面临着让我抓狂的问题。我希望有人能解释我这种奇怪的意图行为。所以我想将 JSON 字符串 {"latitude":53.86907815,"longitude":10.66554789,"formatted":"23.04.2015 10:16:49","route":4}
传递给我的服务 class "PostData" 但我得到 null 以及 onStartCommand() 方法中的 JSON 字符串同时,格式化值始终是旧值,而不是我传递给 class 的当前时间。
MainActivity内部class的调用部分:
String jSONString = convertToJSON(pLong, pLat, formatted);
Intent intentJson = new Intent(MainActivity.this, PostData.class);
intentJson.putExtra("json_data", jSONString);
startService(intentJson);
PostDataclass:
public class PostData extends IntentService {
public PostData() {
super("someone");
// TODO Auto-generated constructor stub
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
return super.onStartCommand(intent, flags, startId);
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
String jSONString = intent.getStringExtra("json_data");
if(jSONString != null){
System.out.println("Output from onStartCommand "+jSONString);
}
}
}
我也尝试调试它,但总是在调试器处于 onStartCommand 时输出为空!!
此屏幕截图是调试器在此行 'startService(intentJson);' 时捕获的:
这是我在 onStartCommand 方法中得到的一些输出。这里格式化总是有相同的时间戳:23.04.2015 18:37:49
在调试中 mod 我总是得到 null 作为输出!!
04-24 11:29:22.785: I/System.out(23721): Output from onStartCommand {"latitude":53.86898202,"longitude":10.66561591,"formatted":"24.04.2015 11:29:23","route":1}
04-24 11:29:22.805: I/System.out(23721): Output from onStartCommand {"latitude":53.86907815,"longitude":10.66554789,"formatted":"23.04.2015 18:37:49","route":4}
04-24 11:29:23.766: I/System.out(23721): Output from onStartCommand {"latitude":53.86898202,"longitude":10.66561589,"formatted":"24.04.2015 11:29:24","route":1}
04-24 11:29:23.806: I/System.out(23721): Output from onStartCommand {"latitude":53.86907815,"longitude":10.66554789,"formatted":"23.04.2015 18:37:49","route":4}
04-24 11:29:24.787: I/System.out(23721): Output from onStartCommand {"latitude":53.868982,"longitude":10.66561591,"formatted":"24.04.2015 11:29:25","route":1}
04-24 11:29:24.807: I/System.out(23721): Output from onStartCommand {"latitude":53.86907815,"longitude":10.66554789,"formatted":"23.04.2015 18:37:49","route":4}
04-24 11:29:25.758: I/System.out(23721): Output from onStartCommand {"latitude":53.868982,"longitude":10.66561591,"formatted":"24.04.2015 11:29:26","route":1}
04-24 11:29:25.818: I/System.out(23721): Output from onStartCommand {"latitude":53.86907815,"longitude":10.66554789,"formatted":"23.04.2015 18:37:49","route":4}
04-24 11:29:26.809: I/System.out(23721): Output from onStartCommand {"latitude":53.86907815,"longitude":10.66554789,"formatted":"23.04.2015 18:37:49","route":4}
你必须在intent中设置值并在启动服务时传递该intent,你可以在serivce onStartCommand(Intent intent, int flags, int startId)中获取值。
这就是您必须通过 Intent 传递该整数的方式。
private class StartClick implements View.OnClickListener {
public void onClick(View v) {
Intent intent = new Intent(main_activity,ServiceMP3.class);
intent.putExtras("position",4);
main_activity.startService(intent);
}
}
你可以得到值是这样的服务
public int onStartCommand(Intent intent, int flags, int startId) {
if(intent != null){
int position = intent.getIntExtra("position", 0);
}
}
错误一定是在别的地方。
意图和捆绑工作正常。你可以转达那个!
您是否多次启动 PostData
服务?这可能会造成混淆,因为意图服务就像队列一样工作吗?它一个接一个地处理一个意图,并在前一个工作完成后从新的意图开始。
此外,您应该在 onHandleIntent
而不是 onStartCommand
中处理意图数据。
无需覆盖 onStartCommand
。这可能会导致错误,因为如果 IntentService
已经 运行 并从队列中获取新的 Intent,我不确定将哪个 Intent 作为参数传递给这里。
我正面临着让我抓狂的问题。我希望有人能解释我这种奇怪的意图行为。所以我想将 JSON 字符串 {"latitude":53.86907815,"longitude":10.66554789,"formatted":"23.04.2015 10:16:49","route":4}
传递给我的服务 class "PostData" 但我得到 null 以及 onStartCommand() 方法中的 JSON 字符串同时,格式化值始终是旧值,而不是我传递给 class 的当前时间。
MainActivity内部class的调用部分:
String jSONString = convertToJSON(pLong, pLat, formatted);
Intent intentJson = new Intent(MainActivity.this, PostData.class);
intentJson.putExtra("json_data", jSONString);
startService(intentJson);
PostDataclass:
public class PostData extends IntentService {
public PostData() {
super("someone");
// TODO Auto-generated constructor stub
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
return super.onStartCommand(intent, flags, startId);
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
String jSONString = intent.getStringExtra("json_data");
if(jSONString != null){
System.out.println("Output from onStartCommand "+jSONString);
}
}
}
我也尝试调试它,但总是在调试器处于 onStartCommand 时输出为空!!
此屏幕截图是调试器在此行 'startService(intentJson);' 时捕获的:
这是我在 onStartCommand 方法中得到的一些输出。这里格式化总是有相同的时间戳:23.04.2015 18:37:49
在调试中 mod 我总是得到 null 作为输出!!
04-24 11:29:22.785: I/System.out(23721): Output from onStartCommand {"latitude":53.86898202,"longitude":10.66561591,"formatted":"24.04.2015 11:29:23","route":1}
04-24 11:29:22.805: I/System.out(23721): Output from onStartCommand {"latitude":53.86907815,"longitude":10.66554789,"formatted":"23.04.2015 18:37:49","route":4}
04-24 11:29:23.766: I/System.out(23721): Output from onStartCommand {"latitude":53.86898202,"longitude":10.66561589,"formatted":"24.04.2015 11:29:24","route":1}
04-24 11:29:23.806: I/System.out(23721): Output from onStartCommand {"latitude":53.86907815,"longitude":10.66554789,"formatted":"23.04.2015 18:37:49","route":4}
04-24 11:29:24.787: I/System.out(23721): Output from onStartCommand {"latitude":53.868982,"longitude":10.66561591,"formatted":"24.04.2015 11:29:25","route":1}
04-24 11:29:24.807: I/System.out(23721): Output from onStartCommand {"latitude":53.86907815,"longitude":10.66554789,"formatted":"23.04.2015 18:37:49","route":4}
04-24 11:29:25.758: I/System.out(23721): Output from onStartCommand {"latitude":53.868982,"longitude":10.66561591,"formatted":"24.04.2015 11:29:26","route":1}
04-24 11:29:25.818: I/System.out(23721): Output from onStartCommand {"latitude":53.86907815,"longitude":10.66554789,"formatted":"23.04.2015 18:37:49","route":4}
04-24 11:29:26.809: I/System.out(23721): Output from onStartCommand {"latitude":53.86907815,"longitude":10.66554789,"formatted":"23.04.2015 18:37:49","route":4}
你必须在intent中设置值并在启动服务时传递该intent,你可以在serivce onStartCommand(Intent intent, int flags, int startId)中获取值。
这就是您必须通过 Intent 传递该整数的方式。
private class StartClick implements View.OnClickListener {
public void onClick(View v) {
Intent intent = new Intent(main_activity,ServiceMP3.class);
intent.putExtras("position",4);
main_activity.startService(intent);
}
}
你可以得到值是这样的服务
public int onStartCommand(Intent intent, int flags, int startId) {
if(intent != null){
int position = intent.getIntExtra("position", 0);
}
}
错误一定是在别的地方。 意图和捆绑工作正常。你可以转达那个!
您是否多次启动 PostData
服务?这可能会造成混淆,因为意图服务就像队列一样工作吗?它一个接一个地处理一个意图,并在前一个工作完成后从新的意图开始。
此外,您应该在 onHandleIntent
而不是 onStartCommand
中处理意图数据。
无需覆盖 onStartCommand
。这可能会导致错误,因为如果 IntentService
已经 运行 并从队列中获取新的 Intent,我不确定将哪个 Intent 作为参数传递给这里。