如何使用 SharedPreferences 将广播 Receiver 的结果返回到 MainActivity

How to get back the result from broadcast Receiver to MainActivity by using SharedPreferences

当我使用广播接收器时,我遇到了使用共享首选项将广播接收器的结果发送到 MainActivity 的问题。
MainActivity.java

public class MainActivity extends AppCompatActivity {
    private TextView mBatteryLevelText;
    private ProgressBar mBatteryLevelProgress;
    private BroadcastReceiver mReceiver;
    RelativeLayout relativeLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mBatteryLevelText=(TextView)findViewById(R.id.battery_text);
        mBatteryLevelProgress=(ProgressBar)findViewById(R.id.battery_progress);
        //mReceiver=new BatteryLevelBroadcastReceiver();
        relativeLayout=(RelativeLayout)findViewById(R.id.main_background);
    }
    public void batteryLevelCheck(View view){
        IntentFilter intentFilter=new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        mReceiver=new MyBroadcastClass();
        registerReceiver(mReceiver,intentFilter);
 SharedPreferences sharedPreferences=context.getSharedPreferences(getString(R.integer.BATTERY_LEVEL),Context.MODE_PRIVATE);
int level=Integer.parseInt(getString(R.integer.BATTERY_LEVEL),0);
        if (level<=10){
            mBatteryLevelText.setText(getString(R.string.BATTERY_VERY_DANGER)+":"+level);
            mBatteryLevelProgress.setProgress(level);
            relativeLayout.setBackgroundColor(Color.RED);
        }
        if (level<=20){
            mBatteryLevelText.setText(getString(R.string.BATTERY_DANGER)+":"+level);
            mBatteryLevelProgress.setProgress(level);
            relativeLayout.setBackgroundColor(Color.YELLOW);
        }
        if (level<=10){
            mBatteryLevelText.setText(getString(R.string.BATTERY_OK)+":"+level);
            mBatteryLevelProgress.setProgress(level);
            relativeLayout.setBackgroundColor(Color.BLUE);
        }
        if (level<=10){
            mBatteryLevelText.setText(getString(R.string.BATTERY_FULL)+":"+level);
            mBatteryLevelProgress.setProgress(level);
            relativeLayout.setBackgroundColor(Color.GREEN);
        }
    }

我想从 Broadcast Receiver class 获取 SharedPreferences 的电量,即当前电池电量 我怎样才能从共享偏好中得到这个。 我想在广播接收器中得到这个 class 不允许我分享那个值。
MyBroadcastClass.java

public class MyBroadcastClass extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        int level= intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
        SharedPreferences sharedPreferences=context.getSharedPreferences(getString(R.integer.BATTERY_LEVEL),Context.MODE_PRIVATE);
      SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.putInt("key1",level);
        editor.commit();
        Toast.makeText(context.getApplicationContext(),"Broadcast Received",Toast.LENGTH_LONG).show();
    }
}

这里的共享首选项给出了这样的错误 int 不可能存储在 Sting 中, 我也在绑定这个 getString(R.string.BATTERY_LEVEL);
我的 values/string.xml 文件是这样的

<resources>
    <string name="app_name">BatteryLevelChecking</string>
    <string name="BATTERY_VERY_DANGER">please charge your battery in danger level</string>
    <string name="BATTERY_DANGER">Please charge your battery low</string>
    <string name="BATTERY_OK">Battery ok to use your mobile</string>
    <string name="BATTERY_GOOD">Battery is good</string>
    <string name="BATTERY_FULL">Battery Full please unplug charger</string>
    <integer name="BATTERY_LEVEL">10</integer>

</resources>

这个也给出了同样的错误。 这里有什么问题
以及如何将 int 值保存到字符串文件中

您可以在 activity 中设置 SharedPreferences.OnSharedPreferenceChangeListener。因此,每当 SharedPreferences 有任何变化时,您都会在 activity.

中收到通知

供参考https://developer.android.com/reference/android/content/SharedPreferences.OnSharedPreferenceChangeListener.html