The road to Android automated testing--notification

Source: Internet
Author: User

After Android4.3 is more convenient, inherit the Notificationlistenerservice service class, the class inherits to service, is a service class, when the notification bar has a new notification message delivery, the message has been cleared, A callback is notified to the method below the class.

So there are two ways to rewrite it, onnotificationposted and onnotificationremoved. With this service, you need to declare system permissions in Androidmenifest.xml:

Finally, because it is a system service, we are unable to start it through the StartService (Intent Intent) mode and need to start the service on the phone: security->notification access, set up.

The specific implementation, by notification mnotification = Sbn.getnotification (), gets to the message object notification, and then resolves the individual fields.

Android4.4 new extras can be resolved by Extras.getcharsequence method, while Android4.3 can only get the entire notifcation string to parse through the ToString () method.

So what do you do before 4.3? What can be thought of here is the way of command: if there is a better way to get in touch with me.

ADB Shell Dumpsys Notification

Then use the output stream to parse and get the field of the response. Of course, you can put the code in a timed task, go to fetch it once a time, and have reached the test goal.

The code to process the output stream is as follows:

Testbutton.setonclicklistener (NewView.onclicklistener () { Public voidOnClick (View v) {string[] commands= {"Dumpsys notification"}; Process Process=NULL; DataOutputStream DataOutputStream=NULL; Try{Process= Runtime.getruntime (). EXEC ("su"); DataOutputStream=NewDataOutputStream (Process.getoutputstream ()); intLength =commands.length;  for(inti = 0; i < length; i++) {log.e (TAG,"commands[" + i + "]:" +Commands[i]); Dataoutputstream.writebytes (Commands[i]+ "\ n"); } dataoutputstream.writebytes ("Exit\n");                            Dataoutputstream.flush ();                            Process.waitfor (); BufferedReader Reader=NULL; Reader=NewBufferedReader (NewInputStreamReader (Process.getinputstream ())); String Line= ""; List<String> linelist =NewArraylist<string>(); FinalStringBuilder log =NewStringBuilder (); String Separator= System.getproperty ("Line.separator"); Pattern Pattern= Pattern.compile ("pkg=[^\\s]+");  while(line = Reader.readline ())! =NULL) {                  if(Line! =NULL&& Line.trim (). StartsWith ("Notificationrecord") {Matcher Matcher=Pattern.matcher (line); if(Matcher.find ()) {Linelist.add (Matcher.group ()); }Else{log.e (TAG,"What ' s this?!");                  }} log.append (line);              Log.append (separator); } log.v (TAG,"LOG:" +log.tostring ()); intSize =linelist.size ();  for(inti = 0; i < size; i++) {log.i (TAG,"App:" +Linelist.get (i)); }          } Catch(Exception e) {log.e (TAG,"Copy Fail", E); } finally {              Try {                  if(DataOutputStream! =NULL) {dataoutputstream.close ();              } Process.destroy (); } Catch(Exception e) {}} LOG.V (TAG,"Finish");  }      }); 

The road to Android automated testing--notification

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: info-contact@alibabacloud.com 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.