Search code examples
androidnotificationsradio-button

Notification on/off/options radio buttons


I have a menu button that opens an activity with radio buttons for options.

I got 4 options, notification off, notification with sound, notification with vibrations and notification with sound and vibrations.

EDIT, I changed my code, made some progress, if the if cases are empty, it works fine.

Here is the code:

public class NotificareOptions extends Activity {
    RadioGroup radioGroup;
    RadioButton radio1,radio2,radio3,radio4;
    Button b1;
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.menu_options);
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
         radio1 = (RadioButton) findViewById(R.id.radioFara);
         radio2 = (RadioButton) findViewById(R.id.radioSunet);
         radio3 = (RadioButton) findViewById(R.id.radioVib);
         radio4 = (RadioButton) findViewById(R.id.radioSunetVib);
    //declare the onCheckChangedListener
    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
    //TODO Auto-generated method stub

        if (checkedId == radio1.getId()) {
             } 
        else 
            if (checkedId == radio2.getId()){   
                Notificare.notification.defaults = Notification.DEFAULT_SOUND;
                Notificare.notificationManager.notify(Notificare.uniqueID, Notificare.notification);
             }
        else 
            if (checkedId == radio3.getId()){ 
                long[] vibration = {0,300,300,300};
                   Notificare.notification.vibrate = vibration;
                   Notificare.notificationManager.notify(Notificare.uniqueID, Notificare.notification);
             }
        else
            if (checkedId == radio4.getId()){
                Notificare.notification.defaults = Notification.DEFAULT_SOUND;
                long[] vibration = {0,300,300,300};
                   Notificare.notification.vibrate = vibration;
                Notificare.notificationManager.notify(Notificare.uniqueID, Notificare.notification);
             }

         }
     });

    radioGroup.check(radio2.getId());
}
}

I want to add to the Notification class the lines of code it needs in order to vibrate/make a sound etc when it gets triggered.

Here is the Log:

12-06 15:05:25.932: E/AndroidRuntime(1629): FATAL EXCEPTION: main
12-06 15:05:25.932: E/AndroidRuntime(1629): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.NotificareOptions}: java.lang.NullPointerException
12-06 15:05:25.932: E/AndroidRuntime(1629):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
12-06 15:05:25.932: E/AndroidRuntime(1629):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
12-06 15:05:25.932: E/AndroidRuntime(1629):     at android.app.ActivityThread.access$600(ActivityThread.java:122)
12-06 15:05:25.932: E/AndroidRuntime(1629):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
12-06 15:05:25.932: E/AndroidRuntime(1629):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-06 15:05:25.932: E/AndroidRuntime(1629):     at android.os.Looper.loop(Looper.java:137)
12-06 15:05:25.932: E/AndroidRuntime(1629):     at android.app.ActivityThread.main(ActivityThread.java:4340)
12-06 15:05:25.932: E/AndroidRuntime(1629):     at java.lang.reflect.Method.invokeNative(Native Method)
12-06 15:05:25.932: E/AndroidRuntime(1629):     at java.lang.reflect.Method.invoke(Method.java:511)
12-06 15:05:25.932: E/AndroidRuntime(1629):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-06 15:05:25.932: E/AndroidRuntime(1629):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-06 15:05:25.932: E/AndroidRuntime(1629):     at dalvik.system.NativeStart.main(Native Method)
12-06 15:05:25.932: E/AndroidRuntime(1629): Caused by: java.lang.NullPointerException
12-06 15:05:25.932: E/AndroidRuntime(1629):     at com.example.app.NotificareOptions$1.onCheckedChanged(NotificareOptions.java:45)
12-06 15:05:25.932: E/AndroidRuntime(1629):     at android.widget.RadioGroup.setCheckedId(RadioGroup.java:172)
12-06 15:05:25.932: E/AndroidRuntime(1629):     at android.widget.RadioGroup.access$600(RadioGroup.java:52)
12-06 15:05:25.932: E/AndroidRuntime(1629):     at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:342)
12-06 15:05:25.932: E/AndroidRuntime(1629):     at android.widget.CompoundButton.setChecked(CompoundButton.java:128)
12-06 15:05:25.932: E/AndroidRuntime(1629):     at android.widget.RadioGroup.setCheckedStateForView(RadioGroup.java:179)
12-06 15:05:25.932: E/AndroidRuntime(1629):     at android.widget.RadioGroup.check(RadioGroup.java:163)
12-06 15:05:25.932: E/AndroidRuntime(1629):     at com.example.app.NotificareOptions.onCreate(NotificareOptions.java:65)
12-06 15:05:25.932: E/AndroidRuntime(1629):     at android.app.Activity.performCreate(Activity.java:4465)
12-06 15:05:25.932: E/AndroidRuntime(1629):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-06 15:05:25.932: E/AndroidRuntime(1629):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
12-06 15:05:25.932: E/AndroidRuntime(1629):     ... 11 more

Solution

  • I saved a variable with shared preferences and i used it to achive what i wanted in the notification service class.

            if (checkedId == radio1.getId()) {
                 Editor e1 = prefs.edit();
                 e1.putString(OptionsPos, "1");
                 e1.commit();
                 } 
            else 
                if (checkedId == radio2.getId()){   
                //  Notificare.notification.defaults = Notification.DEFAULT_SOUND;
                //  Notificare.notificationManager.notify(Notificare.uniqueID, Notificare.notification);
                     Editor e1 = prefs.edit();
                     e1.putString(OptionsPos, "2");
                     e1.commit();
                 }
            else 
    ....