“This is the 12th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

Curriculum background

  • It’s almost the end of the year and I’ve got a lot on my plate and nothing to do with it
  • The Nuggets are getting more rewarding againA little joy
  • But the last time I wrote 50 technical documents by hand I felt so hollowed out that I had nothing left to write about
  • I’d like to share a little skill called auto-.js that I got earlier

The text start

In the previous chapter, we got the first red envelope successfully, but we didn’t get the second red envelope. Here we continue

1. Get red envelopes in cycles. We can write an endless loop and line up to check if there are any red envelopes. Jump out of the loop when there are no red envelopes. Perform the following steps.

When the red envelope cannot be found, the red envelope will be displayed. And get out of the search cycle. Specific code

// Look for red packets
while(true){
    hongbao = id("com.tencent.mobileqq:id/h69").findOne(1000)
    if(hongbao){
        click(hongbao.bounds().centerX(),hongbao.bounds().centerY())
        liiy_sleep(1)
    
        // Click the close button
        desc("Closed").findOne(1000).click()
        liiy_sleep(1)
        
        // Delete the red packets already received
        longClick(hongbao.bounds().centerX(),hongbao.bounds().centerY())
        text("Delete").findOne(1000).click()
        let del_btn = text("Delete").findOne(1000)
        if(del_btn){
            click(del_btn.bounds().centerX(),del_btn.bounds().centerY())
            liiy_sleep(0.1)}}else{
        toast("No red envelope found")
        liiy_sleep(1)
        break; }}Copy the code

2. When we receive a red envelope from our current friend. You need to return to the chat interface and wait for the next red envelope. You can execute the back() method directly to simulate the phone’s back button.

That brings us back to the main screen. Keep waiting for the next red envelope. However, our program will only execute once, so how can we keep it waiting to receive red packets? Again, write an infinite loop that keeps listening for the list of messages.

In the next section, we’ll continue to talk about how to keep the program running.