【问题标题】:Pass JsonResponse to IntentService将 JsonResponse 传递给 IntentService
【发布时间】:2017-03-30 12:25:08
【问题描述】:

我有我的MainActivity,我可以得到很好的回应:

 JSONObject responseVal = responseObj.getJSONObject("data");
                message = responseVal.getString("id");
                Log.i(TAG, "onResponse: " + message);

我有 IntentService 并想将 message 值传递给它。

我尝试使用SharedPreferencesintentPutExtra,但它不起作用。

最好的方法是什么?

【问题讨论】:

  • 请出示您的代码

标签: java android android-intent sharedpreferences httpresponse


【解决方案1】:

如果您想将该消息存储到应用程序中而不是用户 sharedprefrences 其他明智的使用 putextragetIntent.getextra() 方法。

在您的点击事件中

Intent i=new Intent(this,yourclass.class);
i.putExtra("demo",message);

yourclass.class

String mes=getIntent().getStringExtra("demo");

【讨论】:

    【解决方案2】:
    1. 要将message 发送到您的IntentService,请尝试以下操作:

      Intent intent = new Intent(MainActivity.this, YourIntentService.class);
      intent.putExtra("message", message);
      startService(intent);
      
    2. 要在IntentService 中接收message,试试这个:

      @Override
      protected void onHandleIntent(Intent intent) {
          String message = intent.getStringExtra("message");
          ........
      }
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-21
      • 1970-01-01
      • 2021-09-27
      • 1970-01-01
      • 1970-01-01
      • 2021-11-19
      相关资源
      最近更新 更多