How can i create a.simple check box in the preferences screen (a checkboxpreferene) to show a notification?what i mean is that i want enter in my application settings(the preferences screen) and there i'll find the checkbox (show notification).when checked start a notification (for now a text like :my first notification). Obviously when is not checked the notification disappear.i can't find any guide about it. This is part of my code so far: the preferences screen:
public class settings extends PreferenceActivity {
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
the settings.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:summary="@string/menu_settings"
android:title="@string/Settings">
<CheckBoxPreference
android:key="firstDependent"
android:summary="@string/clicca_il_primo_check"
android:title="@string/Primocheck"
android:defaultValue="false"
/>
</PreferenceCategory>
<!--Any other categories include here-->
</PreferenceScreen>
and in my MainActivity
@SuppressLint("NewApi")
private void checkPref(Intent intent){
SharedPreferences myPref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
boolean pref_opt1 = myPref.getBoolean("firstDependent", false);
if (pref_opt1){
NotificationManager notifi = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification.Builder(getApplicationContext())
.setContentTitle("Hello Informations")
.setContentText("Hellow world")
.setSmallIcon(R.drawable.icon_small_not)
.setTicker("Helloooo")
.build();
notification.flags = Notification.FLAG_ONGOING_EVENT;
Intent i = new Intent(MainActivity.this, MainActivity.class);
PendingIntent penInt = PendingIntent.getActivity(getApplicationContext(), 0 , i , 0);
notifi.notify(215,notification);
} else {
NotificationManager notifi = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notifi.cancel(215);
}
}
But the notification not start when i check the checkbox.