2016-08-03 5 views
-3

私はすでにアプリケーションにFirebaseプッシュ通知を正常に実装しましたが、ToggleButton/Switchにユーザー選択を保存させるようにしました。つまり、ユーザーがスイッチをアクティブにしたときに、アプリを閉じて、ステータスが自動的に「無効」に変更され、ここに私のコードは次のとおりです。共有環境設定を使用してToggleButton/Switchのステータスを保存するにはどうすればよいですか?

私はいくつかのチュートリアルを試してみたが、私は、これは整理取得カント...事前に

package com.lfcchile; 

import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import android.widget.CompoundButton; 
import android.widget.Switch; 
import android.widget.Toast; 

import com.google.firebase.messaging.FirebaseMessaging; 

/** 
* Created by Jona on 7/25/16. 
*/ 
public class NotificationSettings extends AppCompatActivity { 

    private static final String TAG = "FCM Service"; 
    private Switch switchComunidad, switchBlog, switchEquipo, switchPartidos; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.notification_settings); 

     switchComunidad = (Switch) findViewById(R.id.switchComunidad); 
     switchBlog = (Switch) findViewById(R.id.switchBlog); 
     switchEquipo = (Switch) findViewById(R.id.switchEquipo); 
     switchPartidos = (Switch) findViewById(R.id.switchPartidos); 

     switchComunidad.setTextOn("On"); 
     switchComunidad.setTextOff("Off"); 
     switchBlog.setTextOn("On"); 
     switchBlog.setTextOff("Off"); 
     switchEquipo.setTextOn("On"); 
     switchEquipo.setTextOff("Off"); 
     switchPartidos.setTextOn("On"); 
     switchPartidos.setTextOff("Off"); 

     //Funciones que controlan las acciones de cada Switch 
     switchComunidad.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       if (switchComunidad.isChecked()){ 
        FirebaseMessaging.getInstance().subscribeToTopic("Comunidad"); 
        Log.d(TAG, "Suscrito al tema Comunidad"); 
        Toast.makeText(getApplicationContext(), "Activado Correctamente", 
          Toast.LENGTH_LONG).show(); 
       }else { 
        FirebaseMessaging.getInstance().unsubscribeFromTopic("Comunidad"); 
        Log.d(TAG, "Suscrito al tema Comunidad"); 
        Toast.makeText(getApplicationContext(), "Desactivado Correctamente", 
          Toast.LENGTH_LONG).show(); 
       } 
      } 
     }); 

     switchBlog.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       if (switchBlog.isChecked()){ 
        FirebaseMessaging.getInstance().subscribeToTopic("Blog"); 
        Toast.makeText(getApplicationContext(), "Activado Correctamente", 
          Toast.LENGTH_LONG).show(); 
       }else { 
        FirebaseMessaging.getInstance().unsubscribeFromTopic("Blog"); 
        Toast.makeText(getApplicationContext(), "Desactivado Correctamente", 
          Toast.LENGTH_LONG).show(); 
       } 
      } 
     }); 

     switchEquipo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       if (switchEquipo.isChecked()){ 
        FirebaseMessaging.getInstance().subscribeToTopic("Equipo"); 
        Toast.makeText(getApplicationContext(), "Activado Correctamente", 
          Toast.LENGTH_LONG).show(); 
       }else { 
        FirebaseMessaging.getInstance().unsubscribeFromTopic("Equipo"); 
        Toast.makeText(getApplicationContext(), "Desactivado Correctamente", 
          Toast.LENGTH_LONG).show(); 
       } 
      } 
     }); 

     switchPartidos.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       if (switchPartidos.isChecked()){ 
        FirebaseMessaging.getInstance().subscribeToTopic("Partidos"); 
        Toast.makeText(getApplicationContext(), "Activado Correctamente", 
          Toast.LENGTH_LONG).show(); 
       }else { 
        FirebaseMessaging.getInstance().unsubscribeFromTopic("Partidos"); 
        Toast.makeText(getApplicationContext(), "Desactivado Correctamente", 
          Toast.LENGTH_LONG).show(); 
       } 
      } 
     }); 

    } 
} 

ありがとう!

+1

SharePrefrenceのブールメソッドを使用してトグルまたはスイッチの状態を真/偽に設定し、SharePrefrenceステータスをチェックして、それを自分のトゥーメールに設定してステータスを切り替えるとき –

+0

[SWITCHボタンの状態を保存し、SharedPrefsで状態を回復] (http://stackoverflow.com/questions/31007320/save-switch-button-state-and-recover-state-with-sharedprefs) – Yazan

答えて

0

私は簡単な例を作成します。

private static final String SWITCH_PARTIDOS_STATE = "switchPartidosState"; 
private SharedPreferences sharedPreferences; 
private Switch switchPartidos; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    sharedPreferences = getSharedPreferences("myname", Context.MODE_PRIVATE); 

    switchPartidos.setChecked(sharedPreferences.getBoolean(SWITCH_PARTIDOS_STATE, false)); 

    switchPartidos.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      sharedPreferences.edit().putBoolean(SWITCH_PARTIDOS_STATE, isChecked).commit(); 
      if (isChecked){ 
       FirebaseMessaging.getInstance().subscribeToTopic("Partidos"); 
       Toast.makeText(getApplicationContext(), "Activado Correctamente", 
         Toast.LENGTH_LONG).show(); 
      }else { 
       FirebaseMessaging.getInstance().unsubscribeFromTopic("Partidos"); 
       Toast.makeText(getApplicationContext(), "Desactivado Correctamente", 
         Toast.LENGTH_LONG).show(); 
      } 
     } 
    }); 
} 

希望すると助かります!

+0

作品!何とか今のスイッチはLOLを "オフ"にしません。 –

関連する問題