0

My application is related to alarm. I used material AlertDialog in my application. I choose one value from AlertDialog and press choose button. I want to store that selected values. It value sets until I do not changed it. How do I?

final Dialog dialog = new MaterialDialog.Builder(Settings_Activity.this)
                .title(R.string.full_battery_alarm1)
                .iconRes(R.mipmap.ic_launcher)
                .limitIconToDefaultSize()
                .items(R.array.full_battery_alarm)
                .itemsCallbackSingleChoice(0, new MaterialDialog.ListCallbackSingleChoice() {
                    @Override
                    public boolean onSelection(MaterialDialog mdialog, View view, int pos, CharSequence text) {
                        manager.sessionWork(pos);

                        showToast(pos + " = " + text);
                        int i = Integer.parseInt(text.toString());

                        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(Settings_Activity.this);
                        SharedPreferences.Editor editor = preferences.edit();
                        editor.putString("select full", Integer.toString(i));
                        editor.apply();

                        String abc = preferences.getString("select full", null);

                        final int l = Integer.parseInt(abc);

                        BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
                            public void onReceive(Context context, Intent intent) {
                                context.unregisterReceiver(this);
                                int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
                                int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

                                if (rawlevel >= 0 && scale > 0) {
                                    level = (rawlevel * 100) / scale;
                                    showToast("level full" + level);


                                    if (level == l) {
                                        showToast("level full 2"+level);

                                        Intent i1 = new Intent(Settings_Activity.this, StopActivity.class);
                                        i1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                        startActivity(i1);
                                        finish();
                                    }
                                }

                            }
                        };
                        IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
                        registerReceiver(batteryLevelReceiver, batteryLevelFilter);
                        mdialog.setSelectedIndex(pos); 
                        return true;
                    }
                })
                .positiveText(R.string.choose)
                .show();
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Poonam Desai
  • 85
  • 1
  • 1
  • 8

2 Answers2

0

Store it in shared preference or just use a db line sql lite

0

You can use SharedPreferences like this. Create your SharedPreferences and Editor.

static SharedPreferences sharedData;
static SharedPreferences.Editor editor;

now

sharedData = getSharedPreferences("mySharedPreference", Context.MODE_PRIVATE);
editor = sharedData.edit();

after this to save your value do

editor.putString("valueKey", ""+yourvaluevariable); editor.commit();

Now this value is saved and will be retrieved event when you will start the app later after closing it. To access that value afterwards use

sharedData.getString("valueKey", "");
knownUnknown
  • 869
  • 11
  • 18
  • @PoonamDesai I didn't understood what u mean by *In the above code at which place I get pref.?* and code in my answer or code in your question. – knownUnknown Oct 27 '16 at 09:59