首页 文章

在将其传递给Service类时,会更改intent extra的子字符串

提问于
浏览
0

我面临的问题让我发疯 . 我希望有人可以解释我的意图这种奇怪的行为 . 所以我想将JSON字符串 {"latitude":53.86907815,"longitude":10.66554789,"formatted":"23.04.2015 10:16:49","route":4} 传递给我的Service类"PostData",但是我在onStartCommand()方法中获取null以及JSON字符串,同时格式化值始终是旧的而不是当前时间我正在上课 .

MainActivity内部类中的调用部分:

String jSONString = convertToJSON(pLong, pLat, formatted);
Intent intentJson = new Intent(MainActivity.this, PostData.class);
intentJson.putExtra("json_data", jSONString);
startService(intentJson);

PostData类:

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);
    }

    }

}

我试图调试它,但总是当debuger在onStartCommand时我输出为null!

调试器位于此行时捕获此屏幕截图'startService(intentJson);':
enter image description here

这是我在onStartCommand方法中得到的一些输出 . 这里格式化的始终是相同的timeStamp: 23.04.2015 18:37:49 在调试模式中,我总是得到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}

2 回答

  • 1

    您必须在intent中设置值并在启动服务时传递该intent,并且您可以在seriStce 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);
    
            }
        }
    

    你可以得到这样的服务 Value

    public int onStartCommand(Intent intent, int flags, int startId) {
    if(intent != null){
       int position = intent.getIntExtra("position", 0);
       }
    }
    
  • 0

    错误必须在其他地方 . 意图和捆绑正常工作 . 你可以继续!

    您是否多次启动 PostData 服务?这可能导致混淆,因为意图服务就像一个队列?它在彼此之后处理一个意图,并在完成上一个工作时以新意图开始 .

    此外,您应该处理 onHandleIntent 中的意图数据而不是 onStartCommand . 无需覆盖 onStartCommand . 可能这会导致错误,因为如果 IntentService 已经在运行并且从队列中获取新的Intent,我不确定哪个Intent作为参数传递给这里 .

相关问题