如何使用ToggleButton
示例:
ToggleButton
禁用(关闭)>>>推送通知应该停止
ToggleButton
启用(ON)>>>推送通知Shouid strat
// turn on/off push notification
if (isChecked){
prefNotification.edit().putBoolean(Constant.PREF_KEY, true).commit();
PushService.setDefaultPushCallback(getApplicationContext(),YOUR_CLASS.class);
Toast.makeText(CatalogActivity.this, "turn on", Toast.LENGTH_SHORT).show();
} else{
prefNotification.edit().putBoolean(Constant.PREF_KEY_TOGGLE_NOTIFICATION, false).commit();
PushService.setDefaultPushCallback(getApplicationContext(), null);
Toast.makeText(CatalogActivity.this, "turn off", Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:1)
use this code inside your toggle button listener
if(isChecked)
{
SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
Editor editor = settings.edit();
editor.putString("push", "true");
editor.commit();
}
else
{
SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
Editor editor = settings.edit();
editor.putString("push", "false");
editor.commit();
}
and receiver class
show push notification according to shared Preference Flag