Java Code Examples for android.support.v7.widget.SwitchCompat#setOnCheckedChangeListener()

The following examples show how to use android.support.v7.widget.SwitchCompat#setOnCheckedChangeListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SortCategoryAdapter.java    From v9porn with MIT License 6 votes vote down vote up
@Override
protected void convert(final BaseViewHolder helper, final Category category) {
    helper.setText(R.id.tv_sort_category_name, category.getCategoryName());
    SwitchCompat switchCompat = helper.getView(R.id.sw_sort_category);
    switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            category.setIsShow(isChecked);
        }
    });
    switchCompat.setChecked(category.getIsShow());
    ImageView imageView = helper.getView(R.id.iv_drag_handle);

    imageView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (onStartDragListener != null) {
                //注意:这里down和up都会回调该方法
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    onStartDragListener.startDragItem(helper);
                }
            }
            return false;
        }
    });
}
 
Example 2
Source File: MainActivity.java    From MaterialHome with Apache License 2.0 6 votes vote down vote up
private void initNavView() {
    boolean night = SPUtils.getPrefBoolean(Constant.THEME_MODEL, false);
    if (night) {
        getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    } else {
        getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    }
    MenuItem item = mNavigationView.getMenu().findItem(R.id.nav_theme);
    mNavigationView.getMenu().findItem(R.id.nav_home).setChecked(true);
    mThemeSwitch = (SwitchCompat) MenuItemCompat.getActionView(item).findViewById(R.id.view_switch);
    mThemeSwitch.setChecked(night);
    mThemeSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            SPUtils.setPrefBoolean(Constant.THEME_MODEL, isChecked);
            mThemeSwitch.setChecked(isChecked);
            if (isChecked) {
                getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            } else {
                getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
            }
        }
    });
}
 
Example 3
Source File: GpsInfoActivity.java    From UsbGps4Droid with GNU General Public License v3.0 6 votes vote down vote up
private void setupUI() {
    if (!isDoublePanel()) {
        startSwitch = (SwitchCompat) findViewById(R.id.service_start_switch);
        startSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                sharedPreferences
                        .edit()
                        .putBoolean(USBGpsProviderService.PREF_START_GPS_PROVIDER, isChecked)
                        .apply();
            }
        });
    }

    numSatellites = (TextView) findViewById(R.id.num_satellites_text);
    accuracyText = (TextView) findViewById(R.id.accuracy_text);
    locationText = (TextView) findViewById(R.id.location_text);
    elevationText = (TextView) findViewById(R.id.elevation_text);
    timeText = (TextView) findViewById(R.id.gps_time_text);

    logText = (TextView) findViewById(R.id.log_box);
    logTextScroller = (ScrollView) findViewById(R.id.log_box_scroller);
}
 
Example 4
Source File: DragGridActivity.java    From SwipeRecyclerView-master with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mHeaderView = getLayoutInflater().inflate(R.layout.layout_header_switch, mRecyclerView, false);
    mRecyclerView.addHeaderView(mHeaderView);

    SwitchCompat switchCompat = (SwitchCompat) mHeaderView.findViewById(R.id.switch_compat);
    switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            mRecyclerView.setItemViewSwipeEnabled(isChecked);
        }
    });

    mRecyclerView.setLongPressDragEnabled(true); // 长按拖拽,默认关闭。
    mRecyclerView.setItemViewSwipeEnabled(false); // 滑动删除,默认关闭。
}
 
Example 5
Source File: DefineActivity.java    From SwipeRecyclerView with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    View header = getLayoutInflater().inflate(R.layout.layout_header_switch, mRecyclerView, false);
    mRecyclerView.addHeaderView(header);

    SwitchCompat switchCompat = header.findViewById(R.id.switch_compat);
    switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // 控制是否可以侧滑删除。
            mRecyclerView.setItemViewSwipeEnabled(isChecked);
        }
    });

    mRecyclerView.setLongPressDragEnabled(true); // 长按拖拽,默认关闭。
    mRecyclerView.setItemViewSwipeEnabled(false); // 滑动删除,默认关闭。

    // 自定义拖拽控制参数。
    mRecyclerView.setOnItemMovementListener(mItemMovementListener);
}
 
Example 6
Source File: SwitchBar.java    From AcDisplay with GNU General Public License v2.0 6 votes vote down vote up
@SuppressLint("WrongViewCast")
@Override
protected void onFinishInflate() {
    super.onFinishInflate();
    if (!isInEditMode()) {
        RippleUtils.makeFor(this, false);
    }

    mTextView = (TextView) findViewById(R.id.title);
    mIconView = (ImageView) findViewById(R.id.icon);
    mSwitch = (SwitchCompat) findViewById(R.id.switch_);
    mSwitch.setOnCheckedChangeListener(mListener);
    updateText(mSwitch.isChecked());

    // Toggle switch on click on the panel.
    setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            toggle();
        }
    });
}
 
Example 7
Source File: ToggleNavigationItemDescriptor.java    From material-navigation-drawer with Apache License 2.0 6 votes vote down vote up
private void setup(final View view) {
    SwitchCompat toggle = ViewHolder.get(view, R.id.toggle);
    toggle.setOnCheckedChangeListener(null);
    toggle.setChecked(mChecked);
    toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            mChecked = isChecked;
            updateText(view);

            onChange(view.getContext(), mChecked);
        }
    });

    updateText(view);
}
 
Example 8
Source File: ActorMoviesFragment.java    From Mizuu with Apache License 2.0 6 votes vote down vote up
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.switch_button, menu);

    int padding = MizLib.convertDpToPixels(getActivity(), 16);

    SwitchCompat switchCompat = (SwitchCompat) menu.findItem(R.id.switch_button).getActionView();
    switchCompat.setChecked(mChecked);
    switchCompat.setText(R.string.inLibrary);
    switchCompat.setSwitchPadding(padding);
    switchCompat.setPadding(0, 0, padding, 0);

    switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            mChecked = isChecked;
            mAdapter.notifyDataSetChanged();
        }
    });

    super.onCreateOptionsMenu(menu, inflater);
}
 
Example 9
Source File: EqualizerActivity.java    From Rey-MusicPlayer with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.switch_menu, menu);
    MenuItem menuItem = menu.findItem(R.id.myswitch);
    View view = MenuItemCompat.getActionView(menuItem);
    mToggleEqualizerButton = (SwitchCompat) view.findViewById(R.id.switchButton);
    mToggleEqualizerButton.setChecked(PreferencesHelper.getInstance().getBoolean(PreferencesHelper.Key.IS_EQUALIZER_ACTIVE, false));
    mToggleEqualizerButton.setOnCheckedChangeListener(equalizerActive);
    return true;
}
 
Example 10
Source File: MainActivity.java    From lantern with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SwitchCompat toggle = findViewById(R.id.switch_flash);
    lantern = new Lantern(this)
            .checkAndRequestSystemPermission()
            .observeLifecycle(this);

    // Init Lantern by calling `init()`, which also check if camera permission is granted + camera feature exists
    // In case permission is not granted, request for the permission and retry by calling `init()` method
    // NOTE: In case camera feature is does not exist, `init()` will return `false` and Lantern will not have
    // torch functionality but only screen based features
    if (!lantern.initTorch()) {
        // Request if permission is not granted
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQUEST_CODE);
    }

    toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean state) {
            if (state) {
                // true
                lantern.alwaysOnDisplay(true)
                        .fullBrightDisplay(true)
                        .enableTorchMode(true)
                        .pulse(true).withDelay(1, TimeUnit.SECONDS);
            } else {
                //false
                lantern.alwaysOnDisplay(false)
                        .fullBrightDisplay(false)
                        .enableTorchMode(false).pulse(false);
            }
        }
    });
}
 
Example 11
Source File: MyElectricMainFragment.java    From AndroidApp with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.me_menu, menu);
    super.onCreateOptionsMenu(menu, inflater);
    costSwitch = (SwitchCompat) MenuItemCompat.getActionView(menu.findItem(R.id.cost_switch));
    costSwitch.setOnCheckedChangeListener(checkedChangedListener);
    costSwitch.setChecked(blnShowCost);
}
 
Example 12
Source File: SwitchCompatActivity.java    From twoh-android-material-design with MIT License 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.activity_switch_view);
    switchCompat1 = (SwitchCompat) findViewById(R.id.sw_button);
    switchCompat2 = (SwitchCompat) findViewById(R.id.sw_button2);
    super.onCreate(savedInstanceState);
    setupToolbar();

    checkedChangeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            Toast.makeText(SwitchCompatActivity.this,compoundButton.getText()+" is "+compoundButton.isChecked(),Toast.LENGTH_SHORT).show();
            decideToDisplay();
        }
    };

    switchCompat1.setOnCheckedChangeListener(checkedChangeListener);
    switchCompat2.setOnCheckedChangeListener(checkedChangeListener);

    Button btTutorial = (Button) findViewById(R.id.bt_tutorial);
    btTutorial.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            decideToDisplay();
            readTheTutorial(Const.TUTORIAL_SWITCHCOMPAT);
        }
    });
    decideToDisplay();
}
 
Example 13
Source File: MainActivity.java    From HeartbeatFixerForGCM with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    MenuItem item = menu.findItem(R.id.action_switch);
    mSwitch = (SwitchCompat) item.getActionView();
    mSwitch.setOnCheckedChangeListener(this);
    mSwitch.setChecked(HeartbeatFixerUtils.isHeartbeatFixerEnabled(this));
    return true;
}
 
Example 14
Source File: SpringSettingsBottomDialog.java    From CircularReveal with MIT License 5 votes vote down vote up
public void addSwitch(String label, boolean defaultState,
    final CompoundButton.OnCheckedChangeListener listener) {
  if (switchAdded) {
    return;
  }

  switchAdded = true;

  final SwitchCompat switchView = new SwitchCompat(getContext());
  switchView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
      listener.onCheckedChanged(buttonView, isChecked);

      if (springManagerAdded) {
        stiffnessView.setEnabled(isChecked);
        dampingView.setEnabled(isChecked);
      }
    }
  });
  switchView.setChecked(defaultState);
  switchView.setText(label);

  addView(switchView, createMarginLayoutParams(MATCH_PARENT, WRAP_CONTENT, 0, 0, 0, dp(16)));

  if (springManagerAdded) {
    stiffnessView.setEnabled(defaultState);
    dampingView.setEnabled(defaultState);
  }
}
 
Example 15
Source File: TMSLayerSettingsActivity.java    From android_maplibui with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_raster_layer_style, container, false);

    SwitchCompat switchCompat = (SwitchCompat) v.findViewById(R.id.make_grayscale);
    switchCompat.setChecked(mForceToGrayScale);
    switchCompat.setOnCheckedChangeListener(
            new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    mForceToGrayScale = isChecked;
                }
            });

    mContrastLabel = (TextView) v.findViewById(R.id.contrast_seek);
    SeekBar contrastPicker = (SeekBar) v.findViewById(R.id.contrastSeekBar);
    contrastPicker.setOnSeekBarChangeListener(this);
    contrastPicker.setProgress((int) mContrast * 10);

    mBrightnessLabel = (TextView) v.findViewById(R.id.brightness_seek);
    SeekBar brightnessPicker = (SeekBar) v.findViewById(R.id.brightnessSeekBar);
    brightnessPicker.setOnSeekBarChangeListener(this);
    brightnessPicker.setProgress((int) mBrightness + 255);

    mAlphaLabel = (TextView) v.findViewById(R.id.alpha_seek);
    SeekBar alphaPicker = (SeekBar) v.findViewById(R.id.alphaSeekBar);
    alphaPicker.setOnSeekBarChangeListener(this);
    alphaPicker.setProgress(mAlpha);

    return v;
}
 
Example 16
Source File: MoreFragment.java    From GankLock with GNU General Public License v3.0 5 votes vote down vote up
@Override protected void initView() {
    mToolbar = (Toolbar) mContext.findViewById(R.id.toolbar_more_fragment);
    mTitle = (TextView) mContext.findViewById(R.id.more_fragment_title);
    mItemLockSetting = (TextView) mContext.findViewById(R.id.more_fragment_item_lock_setting);
    mItemLockSetting.setOnClickListener(this);
    mItemStyleSetting = (TextView) mContext.findViewById(R.id.more_fragment_item_style_setting);
    mItemStyleSetting.setOnClickListener(this);
    mItemFeedBack = (TextView) mContext.findViewById(R.id.more_fragment_item_feedback);
    mItemFeedBack.setOnClickListener(this);
    mItemOpenSource = (TextView) mContext.findViewById(R.id.more_fragment_item_open_source);
    mItemOpenSource.setOnClickListener(this);
    mItemEvaluate = (TextView) mContext.findViewById(R.id.more_fragment_item_evaluate);
    mItemEvaluate.setOnClickListener(this);
    mItemAbout = (TextView) mContext.findViewById(R.id.more_fragment_item_about);
    mItemAbout.setOnClickListener(this);
    mSwitch = (SwitchCompat) mContext.findViewById(R.id.more_fragment_switch_button);
    mSwitch.setChecked(PreferenceUtil.getBoolean(Config.GANK_LOCK_IS_OPEN));//根据存储的布尔值来判断是否是开启状态
    mSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                ToastUtil.showToastShort(mContext, "open锁屏");
                PreferenceUtil.putBoolean(Config.GANK_LOCK_IS_OPEN, true);
                //LockManager.startLock(true);//立即显示锁屏
                MyApplication.getContext().startService(new Intent(MyApplication.getContext(), LockService.class));
            } else {
                ToastUtil.showToastShort(mContext, "close锁屏");
                PreferenceUtil.putBoolean(Config.GANK_LOCK_IS_OPEN, false);
                MyApplication.getContext().stopService(new Intent(MyApplication.getContext(), LockService.class));
                //LockManager.cancleLock();
            }
        }
    });
}
 
Example 17
Source File: MainActivity.java    From MagicaSakura with Apache License 2.0 5 votes vote down vote up
public ViewHolderLabel(View itemView) {
    super(itemView);
    title = (TextView) itemView.findViewById(R.id.title);
    content = (TextView) itemView.findViewById(R.id.prompt);
    switchCompat = (SwitchCompat) itemView.findViewById(R.id.switch_button);
    switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            title.setCompoundDrawablesWithIntrinsicBounds(
                    !isChecked ? R.drawable.selector_lock : R.drawable.selector_unlock, 0, 0, 0);
            content.setText(!isChecked ? R.string.textview_click_before : R.string.textview_click_after);
        }
    });
}
 
Example 18
Source File: ActivityAlarmSettings.java    From AlarmOn with Apache License 2.0 5 votes vote down vote up
void populateFrom(Setting setting) {
          ((TextView) row.findViewById(R.id.setting_name)).
                  setText(setting.name());

          if (setting.name().equalsIgnoreCase(getString(R.string.vibrate))) {
              SwitchCompat vibrateSwitch = (SwitchCompat) row.findViewById(
                      R.id.setting_vibrate_sc);

              if (settings.getVibrate()) {
                  vibrateSwitch.setChecked(true);
              } else {
                  vibrateSwitch.setChecked(false);
              }

              vibrateSwitch.setOnCheckedChangeListener(
                      new CompoundButton.OnCheckedChangeListener() {
                  @Override
                  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                      if (isChecked) {
                          settings.setVibrate(true);
                      } else {
                          settings.setVibrate(false);
                      }
                  }
              });
          } else {
              ((TextView) row.findViewById(R.id.setting_value)).
                      setText(setting.value());
          }
}
 
Example 19
Source File: MainActivity.java    From sensey with Apache License 2.0 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    hasRecordAudioPermission = RuntimePermissionUtil.checkPermissonGranted(this, recordAudioPermission);

    // Init Sensey
    Sensey.getInstance().init(this);

    // Init UI controls,views and handler
    handler = new Handler();
    txtViewResult = findViewById(R.id.textView_result);

    swt1 = findViewById(R.id.Switch1);
    swt1.setOnCheckedChangeListener(this);
    swt1.setChecked(false);

    swt2 = findViewById(R.id.Switch2);
    swt2.setOnCheckedChangeListener(this);
    swt2.setChecked(false);

    swt3 = findViewById(R.id.Switch3);
    swt3.setOnCheckedChangeListener(this);
    swt3.setChecked(false);

    swt4 = findViewById(R.id.Switch4);
    swt4.setOnCheckedChangeListener(this);
    swt4.setChecked(false);

    swt5 = findViewById(R.id.Switch5);
    swt5.setOnCheckedChangeListener(this);
    swt5.setChecked(false);

    swt6 = findViewById(R.id.Switch6);
    swt6.setOnCheckedChangeListener(this);
    swt6.setChecked(false);

    swt7 = findViewById(R.id.Switch7);
    swt7.setOnCheckedChangeListener(this);
    swt7.setChecked(false);

    swt8 = findViewById(R.id.Switch8);
    swt8.setOnCheckedChangeListener(this);
    swt8.setChecked(false);

    swt9 = findViewById(R.id.Switch9);
    swt9.setOnCheckedChangeListener(this);
    swt9.setChecked(false);

    swt10 = findViewById(R.id.Switch10);
    swt10.setOnCheckedChangeListener(this);
    swt10.setChecked(false);

    swt11 = findViewById(R.id.Switch11);
    swt11.setOnCheckedChangeListener(this);
    swt11.setChecked(false);

    swt12 = findViewById(R.id.Switch12);
    swt12.setOnCheckedChangeListener(this);
    swt12.setChecked(false);

    swt13 = (SwitchCompat) findViewById(R.id.Switch13);
    swt13.setOnCheckedChangeListener(this);
    swt13.setChecked(false);

    swt14 = (SwitchCompat) findViewById(R.id.Switch14);
    swt14.setOnCheckedChangeListener(this);
    swt14.setChecked(false);

    swt15 = (SwitchCompat) findViewById(R.id.Switch15);
    swt15.setOnCheckedChangeListener(this);
    swt15.setChecked(false);

    Button btnTouchEvent = findViewById(R.id.btn_touchevent);
    btnTouchEvent.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(MainActivity.this, TouchActivity.class));
        }
    });
}
 
Example 20
Source File: ActivityMain.java    From Android-Firewall with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "Create");

    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    setTheme(prefs.getBoolean("dark_theme", false) ? R.style.AppThemeDark : R.style.AppTheme);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    running = true;

    // Action bar
    View view = getLayoutInflater().inflate(R.layout.actionbar, null);
    getSupportActionBar().setDisplayShowCustomEnabled(true);
    getSupportActionBar().setCustomView(view);

    // On/off switch
    SwitchCompat swEnabled = (SwitchCompat) view.findViewById(R.id.swEnabled);
    swEnabled.setChecked(prefs.getBoolean("enabled", false));
    swEnabled.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                Log.i(TAG, "Switch on");
                Intent prepare = VpnService.prepare(ActivityMain.this);
                if (prepare == null) {
                    Log.e(TAG, "Prepare done");
                    onActivityResult(REQUEST_VPN, RESULT_OK, null);
                } else {
                    Log.i(TAG, "Start intent=" + prepare);
                    try {
                        startActivityForResult(prepare, REQUEST_VPN);
                    } catch (Throwable ex) {
                        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
                        onActivityResult(REQUEST_VPN, RESULT_CANCELED, null);
                        Toast.makeText(ActivityMain.this, ex.toString(), Toast.LENGTH_LONG).show();
                    }
                }
            } else {
                Log.i(TAG, "Switch off");
                prefs.edit().putBoolean("enabled", false).apply();
                BlackHoleService.stop(ActivityMain.this);
           }
        }
    });

    // Listen for preference changes
    prefs.registerOnSharedPreferenceChangeListener(this);

    // Fill application list
    fillApplicationList();

    // Listen for connectivity updates
    IntentFilter ifConnectivity = new IntentFilter();
    ifConnectivity.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
    registerReceiver(connectivityChangedReceiver, ifConnectivity);

    // Listen for added/removed applications
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
    intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
    intentFilter.addDataScheme("package");
    registerReceiver(packageChangedReceiver, intentFilter);
}