0
@Override
    public void onAccessibilityEvent(final AccessibilityEvent event) {

        Date date = new Date(event.getEventTime());
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy hh:mm");
        String time = format.format(date);
        String reqTime = "25/11/2018 04:39";
        if (reqTime.equals(time)) {
            Log.d("MyAccessibilityService", "onAccessibilityEvent");

            if (getRootInActiveWindow() == null) {
                return;
            }


            AccessibilityNodeInfoCompat rootInActiveWindow = AccessibilityNodeInfoCompat.wrap(getRootInActiveWindow());
            //Inspect app elements if ready

            //Search bar is covered with textview which need to be clicked
            List<AccessibilityNodeInfoCompat> clickOnQuestionMark = rootInActiveWindow.findAccessibilityNodeInfosByViewId("com.whatsapp:id/menuitem_search");
            if (clickOnQuestionMark.isEmpty() || clickOnQuestionMark == null) {
                return;
            }
            AccessibilityNodeInfoCompat clickMark = clickOnQuestionMark.get(0);
            clickMark.performAction(AccessibilityNodeInfoCompat.ACTION_CLICK);

I am using this code for simuating clicking whatsapp search button, but when I opened whatsapp window on 25-11-2018 at 4:39 nothing happened. The code was working fine when no time was alloted. But problem was that everytime whatsapp was opened the search button would get clicked. How to click on the search button only when whatsapp is opened at a specific time?

dferenc
  • 7,163
  • 12
  • 36
  • 42
Shivansh Khare
  • 55
  • 1
  • 10

1 Answers1

0

You can convert to LocalDate

LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

and use equals method to specific LocalDate

Compares this LocalDate with another ensuring that the date is the same.

Only objects of type LocalDate are compared, other types return false.

Community
  • 1
  • 1
user7294900
  • 47,183
  • 17
  • 74
  • 157
  • it says minimum api required for using toInstant() is 26 and above but most of the phones are running on api 24 so app would become useless for most of the phones.Any way to solve this? – Shivansh Khare Nov 25 '18 at 12:41
  • @ShivanshKhare if you can't upgrade, see https://stackoverflow.com/questions/49348769/offsetdatetime-now-requires-api-26 – user7294900 Nov 25 '18 at 12:42
  • the software is licensed,I am downloading another emualator to try it. But isn't there another way to trigger onAccessibilityEvent() actions only when we want them to and not whenever whatsapp is opened?https://play.google.com/store/apps/details?id=com.codefish.sqedit&hl=en_US . How do apps like this do it? or https://play.google.com/store/apps/details?id=horizonstack.whatsapp.reminder&hl=en_US – Shivansh Khare Nov 25 '18 at 12:59