使用共享偏好存储按钮状态,但当我离开应用程序时,按钮的名称会改变。

问题描述 投票:0回答:1

我把按钮的状态存储在一个共享的首选项中,它似乎可以工作。如果我打开服务并离开应用程序,我可以再次按下按钮来关闭它,这是很好的。唯一的事情是,当我打开服务时,按钮中的文本应该改变为 "停止服务",它确实如此,但当我退出应用程序时,它又回到了 "启动服务",但状态被保存。


  SharedPreferences prefs =  Home.this.getActivity().getSharedPreferences("check", MODE_PRIVATE);
  final String check_state = prefs.getString( "state", "default");
  holder.startsCar.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(check_state.equals("false")){
                            holder.startsCar.setText("Stop Car");
                            Intent i = new Intent(Home.this.getActivity(), MQTTService.class);
                            ContextCompat.startForegroundService(Home.this.getActivity(), i);
                            for (int j = 0; j < adapter.getItemCount(); j++) {
                                if (adapter1.getItem(j).isSelected(true)) {
                                     name = ((TextView) holder.itemView.findViewById(R.id.make)).getText().toString();
                                    Toast.makeText(getActivity(), "Data Inserted....." + name, Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(getActivity(), "Error", Toast.LENGTH_LONG).show();
                                }
                            }
                            SharedPreferences.Editor editor = Home.this.getActivity().getSharedPreferences("check", MODE_PRIVATE).edit();
                            editor.putString("state", "true");
                            editor.apply();
                            adapter.notifyDataSetChanged();

                            //Toast.makeText(Home.this.getContext(), "Service Started", Toast.LENGTH_SHORT).show();
                        }else{
                            holder.startsCar.setText("Start Car");
                            Intent i = new Intent(Home.this.getActivity(), MQTTService.class);
                            getActivity().stopService(i);
                            Toast.makeText(Home.this.getContext(), "Service Stoped", Toast.LENGTH_SHORT).show();
                            SharedPreferences.Editor editor =  Home.this.getActivity().getSharedPreferences("check", MODE_PRIVATE).edit();
                            editor.putString("state", "false");
                            editor.apply();
                            adapter.notifyDataSetChanged();
                        }
                    }
                });

为什么会出现这种情况?

android android-studio android-recyclerview sharedpreferences
1个回答
0
投票

使用这个代码 。

SharedPreferences prefs =  Home.this.getActivity().getSharedPreferences("check", MODE_PRIVATE);
  final String check_state = prefs.getString( "state", "default");

   if(!check_state.equals("false") {
        holder.startsCar.setText("Stop Car"); 
   }else { 
        holder.startsCar.setText("Start Car"); 
   }

  holder.startsCar.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(check_state.equals("false")){
                            holder.startsCar.setText("Stop Car");
                            Intent i = new Intent(Home.this.getActivity(), MQTTService.class);
                            ContextCompat.startForegroundService(Home.this.getActivity(), i);
                            for (int j = 0; j < adapter.getItemCount(); j++) {
                                if (adapter1.getItem(j).isSelected(true)) {
                                     name = ((TextView) holder.itemView.findViewById(R.id.make)).getText().toString();
                                    Toast.makeText(getActivity(), "Data Inserted....." + name, Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(getActivity(), "Error", Toast.LENGTH_LONG).show();
                                }
                            }
                            SharedPreferences.Editor editor = Home.this.getActivity().getSharedPreferences("check", MODE_PRIVATE).edit();
                            editor.putString("state", "true");
                            editor.apply();
                            adapter.notifyDataSetChanged();

                            //Toast.makeText(Home.this.getContext(), "Service Started", Toast.LENGTH_SHORT).show();
                        }else{
                            holder.startsCar.setText("Start Car");
                            Intent i = new Intent(Home.this.getActivity(), MQTTService.class);
                            getActivity().stopService(i);
                            Toast.makeText(Home.this.getContext(), "Service Stoped", Toast.LENGTH_SHORT).show();
                            SharedPreferences.Editor editor =  Home.this.getActivity().getSharedPreferences("check", MODE_PRIVATE).edit();
                            editor.putString("state", "false");
                            editor.apply();
                            adapter.notifyDataSetChanged();
                        }
                    }
                });

请注意,我是通过在开始时使用prefs来设置startCar文本的状态。

© www.soinside.com 2019 - 2024. All rights reserved.