我在哪裏「致電」我的通知?我希望點擊複選框首選項時顯示。 編輯 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
,只有當我從喜好屏幕出去時纔有效。我怎麼做我想要的?
所以在我的情況,我該怎麼辦呢?現在我正在尋找解決這個問題的答案。請你能幫助我嗎?我是否需要將所有'sendSimpleNotification'代碼插入'onCheckedChanged'? –
好吧,把我粘貼到'onCreate()'方法中的代碼(這將在開始時設置監聽器,當點擊複選框時,onCheckedChanged內部的代碼將被執行,你可以用一些。if語句,如果你喜歡,到最適合您 – g00dy
你的意思是這樣:'CB =(複選框)findViewById(R.id.checkBox); cb.setOnCheckedChangeListener(新CompoundButton.OnCheckedChangeListener(){ @覆蓋 公衆void onCheckedChanged(CompoundButton buttonView,boolean isChecked){ sendSimpleNotification(); } }'在onCreate?我不知道我是否理解正確 –