0

i use this code to set from a preferences screen, if i want show the notifications or not. The problem is that this code works only if i create a button to go on preferences screen. I want go on preferences with my sub-menu "settings" button and not a button inside my layout. Thsi is the code

       // prefer
         setPref.setOnClickListener(new Button.OnClickListener(){    
                @Override    
                public void onClick(View arg0) {     
                    // TODO Auto-generated method stub   
                    Intent intent = new Intent(

                            MainActivity    .this, 

                            settings.class);

                    startActivityForResult(intent, 0);

                }});         

            checkPref();

        }

        @SuppressLint("NewApi")
        private void checkPref(){

            SharedPreferences myPref 

            = PreferenceManager.getDefaultSharedPreferences(

                    MainActivity.this);

            boolean pref_opt1 = myPref.getBoolean("pref_opt1", false); 


            if (pref_opt1){
                NotificationManager notifi = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
                Notification notification = new Notification.Builder(getApplicationContext())
                .setContentTitle("Battery Informations")
                .setContentText("Batteria al")
                .setSmallIcon(R.drawable.icon_small_not)
                //.setLargeIcon(aBitmap)
                .setTicker("Livello")
                .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);
                }
        }



        @Override

        protected void onActivityResult(int requestCode, int resultCode, Intent data) {

            // TODO Auto-generated method stub

            super.onActivityResult(requestCode, resultCode, data);

            checkPref();

    }

How can i do it?

Bishan
  • 15,211
  • 52
  • 164
  • 258
David_D
  • 1,404
  • 4
  • 31
  • 65

2 Answers2

0

I want go on preferences with my sub-menu "settings" button and not a button inside my layout.

What you are looking for is Options Menu, you can start learning about menu by reading some of the links below:

Community
  • 1
  • 1
Ahmad Kayyali
  • 8,233
  • 13
  • 49
  • 83
  • Thanks.. But you mean entire code or only a part? From the `@Override public void onClick(View arg0)...` or something else? – David_D Jul 09 '13 at 08:05
0

I hope I understand the question correctly: instead of clicking a button, you would like to start your activity when clicking a menu item. If so, then you can move the code from OnClickListener.onClick() to a method that handles menu click events as described here.

Y2i
  • 3,748
  • 2
  • 28
  • 32
  • So you mean that the entire code i have to move it inside the `onOptionsItemSelected` method? but wich part of code have i to move? – David_D Jul 09 '13 at 08:04
  • Only code that start the settings.class activity - the one in Button.OnClickListener.onClick() – Y2i Jul 10 '13 at 05:57