Где мне позвонить? Я хочу, чтобы это появилось, когда нажми на checkboxpreferenced. редактировать 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);
}
}
это структура моего code..and конечно у меня есть XML, где внутри есть checkboxpreferences с идентификатором и ключом firstDependent
.
конец редактирование. Я пробовал в onResume
и работает только в том случае, если я выхожу из экрана настроек. Как я могу делать то, что хочу?
так в моем случае, как я могу это сделать? это почти 3 месяца, что я ищу ответ на эту проблему. Не могли ли вы помочь мне, пожалуйста? мне вставить весь код 'sendSimpleNotification' в' onCheckedChanged'? –
Ну, поместите код, который я вставил в метод 'onCreate()' (это приведет к установке слушателя в начале. Когда этот флажок будет нажат, будет выполнен код внутри 'onCheckedChanged'. Вы можете его разработать с помощью некоторых ., если заявления, если вы хотите, чтобы удовлетворить вас лучше – g00dy
вы имеете в виду что-то вроде: 'куб = (CheckBox) findViewById (R.id.checkBox); cb.setOnCheckedChangeListener (новый CompoundButton.OnCheckedChangeListener() { @Override общественности аннулированию onCheckedChanged (CompoundButton buttonView, логическое IsChecked) { sendSimpleNotification();} } 'В OnCreate я не знаю, если я имею право понимать –