2019-04-12 service中修改Fragment的ui

service 中:

   //更新ui : handler & messa
        Message msg = new Message();
        msg.what = Constant.UPDATE_TEMPERATURE_HUMIDITY;
        msg.obj = HexUtil.encodeHexStr(data);  //String类型
        HomeFragment.handler.sendMessage(msg);

Fragment 中:

public static Handler handler;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_home, container, false);
        Button rdmindTest = view.findViewById(R.id.send_notice);
        final TextView lastTime = view.findViewById(R.id.last_time);
        //更新ui的handler
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg){
                if(msg.what == Constant.UPDATE_TEMPERATURE_HUMIDITY)
                {
                    lastTime.setText( (String) msg.obj);
                    //lastTime.append("更新ui:"+ i +"\r\n");
                }
            }
        };
...

你可能感兴趣的:(2019-04-12 service中修改Fragment的ui)