1

我在哪裏「致電」我的通知?我希望點擊複選框首選項時顯示。 編輯 MainActivity.java哪裏打電話給我的通知?

import...//here all imports i need 

public class MainActivity extends Activity { 
CheckBoxPreference firtsDependent; 
... 
public void onCreate(Bundle savedInstanceState){ 
    super onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    //and your code 
} 
} 
    private void sendSimpleNotification(){ 

     boolean pref_opt1= PreferenceManager.getDefaultSharedPreferences(MainActivity.this).getBoolean("firstDependent", false); 

     if(pref_opt1) { 
      NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(service.this); 
      notificationBuilder.setContentTitle("Title"); 
      notificationBuilder.setContentText("Context"); 
      notificationBuilder.setTicker("TickerText"); 
      notificationBuilder.setWhen(System.currentTimeMillis()); 
      notificationBuilder.setSmallIcon(R.drawable.ic_stat_icon); 

      Intent notificationIntent = new Intent(this, service.class); 
      PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 

      notificationBuilder.setContentIntent(contentIntent); 
      notificationBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE); 
      mNotificationManager.notify(1, notificationBuilder.build()); 
     } 
     else{ 
      mNotificationManager.cancel(SIMPLE_NOTIFICATION_ID);  
     } 

    } 

的settings.java

import..//all imports i need 
public class settings extends PreferenceActivity{ 
public void onCreate(Bundle savedInstanceState){ 
    super onCreate(savedInstanceState); 
    setContentView(R.xml.settings); 
    } 
} 

這是我有一個xml我當然code..and的結構,其中內部存在checkboxpreferences帶有ID和密碼firstDependent

end edit。 我試過onResume,只有當我從喜好屏幕出去時纔有效。我怎麼做我想要的?

回答

0

你需要一個複選框聽衆像這樣的:

cb = (CheckBox)findViewById(R.id.checkBox); 
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

    @Override 
    public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) { 
     sendSimpleNotification(); 
    } 
} 

對於複選框偏好,使用:

final CheckBoxPreference cbPR= (CheckBoxPreference) getPreferenceManager().findPreference("cbPref"); 

cbPR.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {    
    public boolean onPreferenceChange(Preference preference, Object newValue) { 

     sendSimpleNotification(); 
     return true; 
    } 
}); 
+0

所以在我的情況,我該怎麼辦呢?現在我正在尋找解決這個問題的答案。請你能幫助我嗎?我是否需要將所有'sendSimpleNotification'代碼插入'onCheckedChanged'? –

+0

好吧,把我粘貼到'onCreate()'方法中的代碼(這將在開始時設置監聽器,當點擊複選框時,onCheckedChanged內部的代碼將被執行,你可以用一些。if語句,如果你喜歡,到最適合您 – g00dy

+0

你的意思是這樣:'CB =(複選框)findViewById(R.id.checkBox); cb.setOnCheckedChangeListener(新CompoundButton.OnCheckedChangeListener(){ @覆蓋 公衆void onCheckedChanged(CompoundButton buttonView,boolean isChecked){ sendSimpleNotification(); } }'在onCreate?我不知道我是否理解正確 –

0

在PreferenceActivity的onCreate,註冊喜好變動聽衆:

getSharedPreferences("YourPrefereceFile", 0).registerOnSharedPreferenceChangeListener(this); 

然後,讓你的活動實現OnSharedPreferenceChangeListener。

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, 
     String key) { 
    //Test on the key to know what preference has changed and do what ever you want 

} 

你的代碼修改:

import..//all imports i need 
public class settings extends PreferenceActivity implements OnSharedPreferenceChangeListener{ 
    public void onCreate(Bundle savedInstanceState){ 
     super onCreate(savedInstanceState); 
     setContentView(R.xml.settings); 
     PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this); 
     } 
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, 
       String key) { 
     // test on your wanted key preference 
     // Call your notification methode 

    }  
    } 
+0

是否用於checkbox首選項? –

+0

這是適用於所有您的偏好,它應該適用於您的checkboxpreference,因爲它存儲在SharedPreferences中的布爾值 – user1897934

+0

你能告訴我我的代碼嗎?抱歉,但我現在什麼都不明白。希望你幫我 –