我在哪里可以"调用"我的通知?我希望它在单击复选框xpreferenced时显示。编辑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);
}
}
设置.java
import..//all imports i need
public class settings extends PreferenceActivity{
public void onCreate(Bundle savedInstanceState){
super onCreate(savedInstanceState);
setContentView(R.xml.settings);
}
}
这是我的代码结构。。当然,我有一个xml,里面有带id和关键字firstDependent
的checkbox xpreferences。
结束编辑我在onResume
中尝试过,只有当我从首选项屏幕退出时才能工作。我怎样才能做我想做的事?
您需要一个类似以下的复选框侦听器:
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;
}
});
在您的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
}
}