۱۳۹۴/۰۵/۱۵, ۰۲:۲۲ ب.ظ
سلام
این کد را با کلید 2611 ایجاد میکنید(برای ساخت اطلاعیه ی جدید ) :و برای نمایش و عدم نمایش اطلاعیه با استفاده از سویچ از کد زیر استفاده میکنیم که امکان ذخیره رو هم داخل گذاشتمموفق باشید
این کد را با کلید 2611 ایجاد میکنید(برای ساخت اطلاعیه ی جدید ) :
کد پیاچپی:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher) // notification icon
.setContentTitle("Notification!") // title for notification
.setContentText("kelidestan.com") // message for notification
.setAutoCancel(true); // clear notification after click
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this,0,intent,Intent.FLAG_ACTIVITY_NEW_TASK);
mBuilder.setContentIntent(pi);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
کد پیاچپی:
Switch swch = (Switch)findViewById(R.id.switch);
SharedPreferences shared = getSharedPreferences("Prefs", MODE_PRIVATE);
final SharedPreferences.Editor editor = shared.edit();
boolean boolean_from_sp = shared.getBoolean("myBooleanName", true);
if (boolean_from_sp){
mNotificationManager.notify(0, mBuilder.build());
}else{
mNotificationManager.cancel(0);
}
swch.setChecked(boolean_from_sp);
swch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
mNotificationManager.notify(0, mBuilder.build());
else
mNotificationManager.cancel(0);
editor.putBoolean("myBooleanName", isChecked);
editor.apply();
}
});