OneSignal.setSubscription (true or false) 在这个按钮的帮助下,我使用 SharedPreferences 方法向应用程序添加了一个 Switch 按钮;,但问题是即使我使用 SharedPreferences 不保存 Button 的值,当我们重新启动应用程序时也不保存默认值。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; @Overrideprotected 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();}
添加回答
举报
0/150
提交
取消