借助此按钮,OneSignal.setSubscription(正确或错误)我使用SharedPreferences方法向应用程序中添加了一个“切换”按钮; ,但是问题是,即使我使用SharedPreferences也不保存按钮的值,也不会在我们重新启动应用程序时保存默认值。
package com.package.name;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;
import com.onesignal.OneSignal;
public class ButtonOneSignal extends AppCompatActivity {
Switch btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one_signal); //Layout tanımladım
//switch viettel
btn = (Switch) findViewById(R.id.switch2);
btn.setChecked(getFromSP("sw1"));
btn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked == true) {
OneSignal.setSubscription(true);
Toast.makeText(getApplicationContext(),"Active",Toast.LENGTH_LONG).show();
} else
OneSignal.setSubscription(false);
Toast.makeText(getApplicationContext(),"DeActive",Toast.LENGTH_LONG).show();
}
});
}
private boolean getFromSP(String key) {
SharedPreferences preferences = getApplicationContext().getSharedPreferences("com.package.name", android.content.Context.MODE_PRIVATE);
return preferences.getBoolean(key, false);
}
private void saveInSp(String key, boolean value) {
SharedPreferences preferences = getApplicationContext().getSharedPreferences("com.package.name", android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(key, value);
editor.commit();
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()) {
case R.id.switch2:
saveInSp("sw1", isChecked);
break;
}
}
}
答案 0 :(得分:0)
在按钮侦听器中添加saveInSp
。
btn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
saveInSp("sw1", isChecked);
if (isChecked == true) {
OneSignal.setSubscription(true); Toast.makeText(getApplicationContext(),"Active",Toast.LENGTH_LONG).show();
} else
OneSignal.setSubscription(false);
Toast.makeText(getApplicationContext(),"DeActive",Toast.LENGTH_LONG).show();
}
});
答案 1 :(得分:0)
这样吗?
private void saveInSp(String key, boolean value) {
SharedPreferences preferences = getApplicationContext().getSharedPreferences("com.altaylar.hdfilmizle2017", android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
btn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked == true) {
OneSignal.setSubscription(true);
Toast.makeText(getApplicationContext(),"Aktif",Toast.LENGTH_LONG).show();
} else
OneSignal.setSubscription(false);
Toast.makeText(getApplicationContext(),"DeAktif",Toast.LENGTH_LONG).show();
}
});
editor.putBoolean(key, value);
editor.commit();