کلیدستان

نسخه‌ی کامل: مشکل در نوتیفیکشن
شما در حال مشاهده نسخه آرشیو هستید. برای مشاهده نسخه کامل کلیک کنید.
سلام من یک نوتیفیکشن ساختم که میخوام کاربر بتونه تو تنظیمات اون رو خاموش یا روشن کنه
من تنظیمات رو درست کردم که وقتی swich خاموشه نشون نده وقتی تغییر داد نشون بده که درست کار میکنه اما وقتی دوباره می خوام خاموش بشه خطا میده
swich پیش فرض روی خاموش هست واسه همین درست کا میکنه کد هام رو اینجا میزارم ممنون میشم کسی بتونه کمکی کنه
هرچند مبهم توضیح دادم

اینها صفحه اصلی برنامه که تو oncreat هستند
کد پی‌اچ‌پی:
SharedPreferences notification getSharedPreferences("notiffsett",
                
MODE_PRIVATE);
        
boolean norifbollen notification.getBoolean("notiffk"true);
 
        if (
norifbollen) {
 
            
notification1 = new Notification(R.drawable.ic_launcher,
                    
"برنامه من"System.currentTimeMillis());
 
            
mNotificationManager = (NotificationManagergetSystemService(NOTIFICATION_SERVICE);
 
            
RemoteViews contentView = new RemoteViews(getPackageName(),
                    
R.layout.custom);
            
contentView
                    
.setImageViewResource(R.id.imageR.drawable.ic_launcher);
            
contentView.setTextViewText(R.id.title"نکات مورد نظر");
            
contentView.setTextViewText(R.id.text,
                    
"|در اینجا نکات قرار می گیرد|در اینجا نکات قرار می گیرد|");
            
notification1.contentView contentView;
 
            
Intent notificationIntent = new Intent(thisMain_page.class);
            
PendingIntent contentIntent PendingIntent.getActivity(this0,
                    
notificationIntent0);
            
notification1.contentIntent contentIntent;
 
            
notification1.flags |= Notification.FLAG_ONGOING_EVENT;
            
notification1.flags |= Notification.FLAG_HIGH_PRIORITY;
            
mNotificationManager.notify(1notification1);
        } else {
            
//اشکال برنامه اینجا باید باشه
            // notification1.flags |= Notification.FLAG_AUTO_CANCEL;
        

اینها هم تو اکتیویتی تنظیمات
کد پی‌اچ‌پی:
//in 3 satr to oncreat hast
notification getSharedPreferences("notiffsett"MODE_PRIVATE);
norifbollen notification.getBoolean("notiffk"true);
swnoti.setChecked(norifbollen);
 
//این 3 سطر هم وقتی باتن ذخیره کلیک میشه
SharedPreferences.Editor notiedit notification.edit();
notiedit.putBoolean("notiffk"swnoti.isChecked());
notiedit.commit(); 
بازم ممنون
 
آیا از سرویس استفاده کنم و سرویس و فعال و غیر فعال کنم درست میشه؟
 
سلام
این کد را با کلید 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(thisMainActivity.class);
PendingIntent pi PendingIntent.getActivity(this,0,intent,Intent.FLAG_ACTIVITY_NEW_TASK);
mBuilder.setContentIntent(pi);
NotificationManager mNotificationManager =
                (
NotificationManagergetSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0mBuilder.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(0mBuilder.build());
        }else{
            
mNotificationManager.cancel(0);
        }
        
swch.setChecked(boolean_from_sp);
        
swch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @
Override
            
public void onCheckedChanged(CompoundButton buttonViewboolean isChecked) {
                if (
isChecked)
                    
mNotificationManager.notify(0mBuilder.build());
                 else
                    
mNotificationManager.cancel(0);

                
editor.putBoolean("myBooleanName"isChecked);
                
editor.apply();
            }
        }); 
موفق باشید