Robotium broadcast and service for cross-process automated testing through broadcast and service +shell commands

Source: Internet
Author: User

Remember when I first learned robotium, someone mentioned this method, at that time feel very good, temporarily do not want to contact (in fact, is more in-depth, difficult to quickly understand the fear of knowledge). Now to have to understand, but also to bite the bullet ...

First of all I understand this approach: the entire Android program, we can be seen as a laboratory (process), we use Robotium to belong to one of the laboratories, the laboratory regulations are very strict, do not allow personnel to enter other laboratories. But people always want some news or some activity that requires other lab mates (cross-process). This time we only have one way, is through the transit personnel (service) to inform other laboratories we need them to cooperate, and the specific process to inform them, the other laboratories (ADB) completed the operation we need them to cooperate, the transit personnel come back to inform us, we can start the next work. That's pretty much how it works.

Roughly speaking, the whole process has several priorities: 1. How did the service come about? 2. How the service received your request.

In our lab, it's convenient to have a transit person come to you with a bell, and then put the information you want to tell and which lab to record. Very convenient.

In the Android world, the way is a little different.

The start of the service, found on the internet, there are two methods, one is in the use case of the setup to start, a boot on the start of the service. Here we choose the first type, the second one has not been tested, so dare not to be able to be successful.

Of course, our article is to explore the connection between radio and service, in the next few posts, I will slowly I understand all to tell you, slowly peel off the veil of Robotium cross-process.

So I'm going to use a simple way to tell everyone about the radio and the service, and also to make a collation and record for myself.

First, the principle: in the Android program, we first need a service, above said, we choose to start our service in Setup. Second, after sending the broadcast, a receiver is required to receive the broadcast, and the receiver will execute the code in the onreceive after it receives the broadcast. The idea: How to send a broadcast? Receiver will receive the broadcast (how to receive?). ), what should the code in OnReceive do? This article only covers the first two.

OK, a little bit, may have said the wrong, but roughly the same. So let's look at the first step: How to send a broadcast?

According to our guiding ideology, first start the service, with the service we have to carry out other things possible. I'm not adding it in the test code, I'm adding it in a click event, which is the fact that the button starts the service. Intent the first parameter is the current activity, and the second is what you want to start, which can be another activity or a service. StartService (Intent) started the service. Not familiar with the intent concept of friends suggest to look at the Android Foundation, very urgent is the key OK!

1 New Intent (myactivity.  this, MyService. class ); 2 StartService (intent);

In the network communication, we know that everyone must abide by an agreement to communicate with each other, do not abide by the agreement, the network will not let you play! The broadcast and receiver in Android are the same, and the receiver will receive the broadcast only if it validates the password. The password for communication between our receivers and broadcasts is also set using intent, with two lines of code:

1 New Intent (); 2 intent.setaction (""); 3 Sendbroadcast (intent);

Next, we can see that we instantiate a intent, and then the second piece of code is to set the password of our communication, only the receiver's password is also "", the receiver will receive this broadcast, The third piece of code is the code that sends the broadcast. So that we can

It's simple! But I find a lot of information on the Internet to understand slowly, please forgive me this Android rookie it! Now that we have the code to send the broadcast, we need to design the receiver. The implementation of the receiver is slightly more complicated than sending the broadcast code, but it is not difficult, believe me. On the code ...

 1   @Override  2  public  void   OnCreate () { 3  br = new   Bootreceiver ();  4  intentfilter myif = new   Intentfilter ();  5  myif.addaction (""  6   Registerreceiver (BR, myif);  7   . OnCreate ();  8 } 
@Override      Public void OnDestroy () {        unregisterreceiver (BR);         NULL ;         Super . OnDestroy ();    }

The receiver needs to register, please forgive me I did not put the private bootreceiver br This code to copy in, it as a beautiful mistake it ... Register the receiver first we have to have the receiver instance is the third sentence of our first code, and then said, the receiver and broadcast need to have the same password, the fifth sentence is to set the receiver's receive password, and then register the receiver. The second piece of code is to log the receiver out.

Everything looks so good right, we need to broadcast, three pieces of code there, we need the receiver is also very soon registered. Next we will start to write the code of OnReceive, after our receiver receives the broadcast, the program will automatically start the code inside the OnReceive, remember.

 Public class extends broadcastreceiver {        @Override        publicvoid  onreceive (context context, Intent Intent) {            //plus the logic we need to implement        }    }

OK, the receiver's code is all affixed, we just need to create a new service, register our receiver in the service, and then start our service in the Setup code, when performing such actions as taking photos, viewing the library, sending a broadcast, After the service receives this broadcast, it starts executing the code in OnReceive, such as clicking the Take Photo button, which of course requires us to use the shell command. If you are interested in learning about the shell, you can click here to view Xuxu's article, thanks for sharing.

Well, to here how to set up the radio and service contact also introduced almost, said may not be very good, if there is doubt please contact qq:727665171, welcome everyone harassment.

Article for my original, there are many said do not understand there is not the right place, but the creation is not easy, reproduced please be sure to declare, thank you very much.

Robotium broadcast and service for cross-process automated testing through broadcast and service +shell commands

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.