0

I want to make an app that you can select a certain day of the week using a switch and in that day the user receive a notification.

I read some posts here about it but after the channels update I'm not sure if they work.

I am kind of stuck so I don't have a lot of code yet:

 public class MainActivity extends AppCompatActivity {

    Switch segSwitch;
    Switch terSwitch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        segSwitch = findViewById(R.id.switch1);
        terSwitch = findViewById(R.id.switch2);

        Calendar calendar = Calendar.getInstance();

        int day = calendar.get(Calendar.DAY_OF_WEEK);

        switch (day) {

            case Calendar.MONDAY:
                notificate(segSwitch);

            case Calendar.TUESDAY:
                notificate(terSwitch);
        }
    }

    public void notificate(View view) {

        Switch mySwitch = (Switch) view;

        if (mySwitch.isChecked())
        {
            Toast.makeText(this, "Time to put the garbage out!", Toast.LENGTH_LONG).show();
        }

    }
}

1 Answers1

0

You can use normal Android Notifications (https://developer.android.com/guide/topics/ui/notifiers/notifications and https://developer.android.com/training/notify-user/build-notification) to notify informations when the APp is closed, or do a manual work by creating a Custom View (by inflating your custom "R.layout.xxx") and show this Custom View somewhere in the screen without the need to create a fullscreen Activity (pay attention that this kind of View/Window has few limitations that an Activity doesn't has: no OnStart()/OnPause()/OnBackPressed()/etc... events for example):

final WindowManager cWindowManager = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE));
    mPopupView = mInflater.inflate(cLayoutResID, null);
    mPopupView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            width,
            height,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_FULLSCREEN,
            PixelFormat.TRANSLUCENT);
    params.gravity = Gravity.START | Gravity.TOP;
    params.flags |= WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
    params.x = 0;
    params.y = 0;
cWindowManager.addView(mPopupView, params);
emandt
  • 2,547
  • 2
  • 16
  • 20