我在哪里可以“呼叫”我的通知?我希望在单击 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);
}
}
这是我的代码的结构..当然我有一个 xml 里面有带有 id 和 key 的 checkboxpreferences firstDependent
。
结束编辑。
我试过了onResume
,只有当我从首选项屏幕出去时才能工作。我怎么能做我想做的事?