Android自动化测试之路——Notification

在Android4.3以后较为方便,继承NotificationListenerService服务类, 该类继承至Service,是一个服务类,当通知栏有新的Notification消息送达、已有消息清除的时候,会回调通知到该类下面的方法。

所以需要重写它的两个方法,onNotificationPosted与onNotificationRemoved。使用该服务,需要在AndroidMenifest.xml中声明系统权限:

最后,因是系统服务,我们无法通过startService(Intent intent)方式来启动它,需要在手机:设置->安全->Notification access,启动该服务。

具体实现,通过Notification mNotification = sbn.getNotification();获取到该消息对象Notification,然后解析各个字段。

Android4.4新增extras,可通过extras.getCharSequence方法去解析;而Android4.3的只能通过toString()方法获取到整个Notifcation的字符串去解析。

 

那么在4.3以前如何做?这里能想到的是用命令的方式:如果有更好的方式希望能联系我。

adb shell dumpsys notification

然后用输出流去解析,拿到响应的字段。当然,你可以把代码放到一个定时任务中,每隔一段时间去取一次,已到达测试目的。

处理输出流的代码如下:

testButton.setOnClickListener(new View.OnClickListener() {  
    public void onClick(View v) {  
        String[] commands = {"dumpsys notification"};  
        Process process = null;  
        DataOutputStream dataOutputStream = null;  
  
        try {  
            process = Runtime.getRuntime().exec("su");  
            dataOutputStream = new DataOutputStream(process.getOutputStream());  
            int length = commands.length;  
            for (int i = 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 = new BufferedReader(new InputStreamReader(process.getInputStream()));    
            String line = "";  
            List<String> lineList = new ArrayList<String>();  
            final StringBuilder log = new StringBuilder();    
            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());  
              
            int size = lineList.size();  
            for (int i = 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");  
        }  
    });  

 

posted @ 2014-11-27 18:03  xiao.kkp  阅读(1523)  评论(0编辑  收藏  举报