Question

I have a checkbox in my activity_main.xml

and I have the following code:

public class UpdateService extends Service {

    @Override
    public void onCreate() {
        super.onCreate();
        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        BroadcastReceiver mReceiver = new ScreenReceiver();
        registerReceiver(mReceiver, filter);
    }

    @Override
    public void onStart(Intent intent, int startId) {
        boolean screenOn = intent.getBooleanExtra("screen_state", false);
        if (!screenOn) {
            Toast.makeText(getApplicationContext(), "Screen is turned ON. Broadcast Received", Toast.LENGTH_LONG).show();
            //Want to display this toast message only in checkbox is checked
        } else {
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}

I want to display the Toast notification only when the checkbox is checked otherwise it should not be displayed.

Was it helpful?

Solution

CheckBox Chk = (CheckBox) findViewById(R.id.checkbox);
                    Chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                        public void onCheckedChanged(CompoundButton buttonView,
                                boolean isChecked) {
                            if (isChecked) {
                             Toast.makeText(context, "Screen is turned ON. Broadcast Received", Toast.LENGTH_LONG).show();  
                            }
                        }
                    });

Try this this will work fine

OTHER TIPS

Under onCreate

   mSavePassword = (CheckBox) findViewById(R.id.checkBox1);

    mSavePassword.setOnClickListener(this);

In Onclick

    @Override
  public void onClick(View view) 
   {

    switch(view.getId())
    {

    case R.id.checkBox1:

    Toast.makeText(context, "Screen is turned ON. Broadcast Received", Toast.LENGTH_LONG).show();
        break;
    }
   }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top