OneSignal.setSubscription (true or false) with the help of this button I added a Switch button to the application with the SharedPreferences method; , but the problem is that even though I use SharedPreferences does not save the value of the Button does not save the default value when we restart the application.
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;
}
}
}
在您的按钮侦听器中添加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(); } });
So this way?
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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.