1. 程式人生 > >android獲得控制檯log,列印控制檯log

android獲得控制檯log,列印控制檯log

//android手機root,然後列印控制檯的log,從控制檯log中查詢 Finsky的資訊過濾,然後找到包名資訊。
 
文章出處:https://blog.csdn.net/pangzaifei/article/details/70213731 public static void monitorGooglePlayLogInRoot() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Process process = null;
                DataOutputStream os = null;
                try {
                    // Finsky為google play log輸出過濾標籤
                    String cmd = "logcat -s Finsky";
                    // 先切換到root程序
                    process = Runtime.getRuntime().exec("su");
                    os = new DataOutputStream(process.getOutputStream());
                    os.writeBytes(cmd + "\n");
                    os.flush();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                    String line = null;
                    while ((line = bufferedReader.readLine()) != null) {
                        if (line.contains("Selecting account") || line.contains("for package")) {
                            String[] split = line.split("for package ");
                            if (split.length == 2) {
                                String logPart = split[1];
                                String pkName = logPart.substring(0, logPart.lastIndexOf('.'));
                                Log.e("fffpzf", "google play 打開了詳情頁,包名為:" + pkName);
                                // 執行下載邏輯
//                                onEnterDetailPage(pkName);
                            }
                        }
                    }

                } catch (Exception ignored) {
                } finally {
                    try {
                        if (os != null) {
                            os.close();
                        }
                        process.destroy();
                    } catch (Exception e) {
                    }
                }
            }
        }).start();
    }