On / off Toggle按钮时的Android On / Off推送通知

时间:2015-12-03 10:43:57

标签: android

如何使用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();
    }
}

1 个答案:

答案 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