为什么String值只能在Service for Android中的ONStartCommand中获得一个字符串?

时间:2014-11-20 06:17:58

标签: android android-intent android-fragments

我正在尝试将片段中的字符串发送到服务,如下面的代码:

public static class PlaceholderFragment extends Fragment implements OnClickListener{


    public final static String EXTRA_STATUS = "";
    public final static String EXTRA_ADDRESS = "";
    public final static String EXTRA_NAME = "";

    Intent Intent = new Intent(getActivity() , Alarm.class);
    Intent.putExtra(EXTRA_STATUS, "1");
    Intent.putExtra(EXTRA_ADDRESS, "2");
    Intent.putExtra(EXTRA_NAME, "3");

    getActivity().startService(Intent);

但在StartCommand的{​​{1}}。它只获取我设置为Service的最后一个字符串。

intent

在服务中,日志显示:

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("AlarmDialog", "AlarmDialog!!! address = " + intent.getStringExtra(PlaceholderFragment.EXTRA_ADDRESS));
        Log.i("AlarmDialog", "AlarmDialog!!! name = " + intent.getStringExtra(PlaceholderFragment.EXTRA_NAME));
        Log.i("AlarmDialog", "AlarmDialog!!! status = " +intent.getStringExtra(PlaceholderFragment.EXTRA_STATUS)) ;


        return super.onStartCommand(intent, flags, startId);
    }

我错过了什么?

提前致谢。

1 个答案:

答案 0 :(得分:2)

 public final static String EXTRA_STATUS = "";
    public final static String EXTRA_ADDRESS = "";
    public final static String EXTRA_NAME = "";

更改为

 public final static String EXTRA_STATUS = "status";
    public final static String EXTRA_ADDRESS = "address";
    public final static String EXTRA_NAME = "name";

因为它的空字符串最后一个值为3会被标记。在所有情况下都可以看到相同的