我的應用程序與鬧鐘有關。我在我的應用程序中使用了材料AlertDialog
。我從AlertDialog
中選擇一個值並按下選擇按鈕。我想存儲所選的值。它的價值集,直到我沒有改變它。我如何?Android材質對話框和存儲選定值
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();
使用*共享偏好*來存儲該值並使用它。還張貼一些你的代碼,以便我們可以建議。 –
現在告訴上面的代碼有什麼問題。它是否會生成任何*異常*然後發佈*堆棧跟蹤*。 –