Search code examples
androidsharedpreferencesandroid-broadcastandroid-broadcastreceiverandroid-sharedpreferences

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


When i am working with broadcast receivers am facing problem with sending result of broadcast receiver to MainActivity by using shared preferences.
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);
        }
    }

I want to get the level i.e., current battery level from Broadcast Receiver class for SharedPreferences how can i get this from Shared preferences. I am tying to get this in broadcast receiver class not allowed me to share that value.
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();
    }
}

here Shared preferences gives error like this int not possible to store in Sting, am also tying this getString(R.string.BATTERY_LEVEL);
my values/string.xml file like this

<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>

this one also gives same error. what is the problem here
and how can i save the int value into string file


Solution

  • You can set SharedPreferences.OnSharedPreferenceChangeListener in your activity. So whenever there is any change in SharedPreferences you wil get notified inside your activity.

    For reference https://developer.android.com/reference/android/content/SharedPreferences.OnSharedPreferenceChangeListener.html