0

我有一个来自 adb 的 logcat 文件。该文件如下所示,

  09-03 17:12:04.562   479   957 I ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.amazon.mShop.android/com.amazon.mShop.home.HomeActivity } from pid 1230
09-03 17:12:04.687   479  1030 I ActivityManager: Start proc com.amazon.mShop.android for activity com.amazon.mShop.android/com.amazon.mShop.home.HomeActivity: pid=19164 uid=10158 gids={1006, 3003}
09-03 17:12:05.593   479   912 I ActivityManager: Starting: Intent { dat=?paramSuggestionText=cell%20phone flg=0x20000 cmp=com.amazon.mShop.android/com.amazon.mShop.search.SearchActivity (has extras) } from pid 19164
09-03 17:12:05.796   479   620 I ActivityManager: Displayed com.amazon.mShop.android/com.amazon.mShop.search.SearchActivity: +186ms (total +1s119ms)
09-03 17:12:08.601   479   620 I ActivityManager: Displayed com.amazon.mShop.android/com.amazon.mShop.home.HomeActivity: +3s925ms
09-03 17:12:08.601   479   620 I ActivityManager: Resumed Activity: com.amazon.mShop.android/com.amazon.mShop.search.SearchActivity totalTime: 4037 ms
09-03 17:13:35.742   479   957 I ActivityManager: Starting: Intent { act=intentActionStartActivity flg=0x20000 cmp=com.amazon.mShop.android/com.amazon.mShop.categoryBrowse.CategoryBrowseActivity } from pid 19164
09-03 17:13:36.007   479   620 I ActivityManager: Displayed com.amazon.mShop.android/com.amazon.mShop.categoryBrowse.CategoryBrowseActivity: +222ms
09-03 17:13:53.328   479   957 I ActivityManager: Starting: Intent { dat=?&paramSuggestionFilters=s%3An%3A%2C%3A4991425011%0As%3An%3A%2C%3A4991426011%0As%3An%3A%2C%3A5088769011%0As%3An%3A%2C%3A5263615011&paramDepartmentName=Programs flg=0x20000 cmp=com.amazon.mShop.android/com.amazon.mShop.search.SearchActivity (has extras) } from pid 19164
09-03 17:13:53.484   479   620 I ActivityManager: Displayed com.amazon.mShop.android/com.amazon.mShop.search.SearchActivity: +145ms
09-03 17:14:54.585   479   484 I ActivityManager: Starting: Intent { cmp=com.amazon.mShop.android/com.amazon.mShop.barcodeSearch.BarcodeFirstTryTipActivity } from pid 19164
09-03 17:14:54.718   479   620 I ActivityManager: Displayed com.amazon.mShop.android/com.amazon.mShop.barcodeSearch.BarcodeFirstTryTipActivity: +102ms
09-03 17:15:30.210   479   957 I ActivityManager: Starting: Intent { flg=0x20000 cmp=com.amazon.mShop.android/com.amazon.mShop.search.SearchActivity (has extras) } from pid 19164
09-03 18:03:02.140   479   967 I ActivityManager: Starting: Intent { act=intentActionStartActivity flg=0x4000000 cmp=com.amazon.mShop.android/com.amazon.mShop.home.HomeActivity } from pid 19164
09-03 18:03:08.054   479  1005 I ActivityManager: Starting: Intent { dat= flg=0x20000 cmp=com.amazon.mShop.android/com.amazon.mShop.search.SearchActivity (has extras) } from pid 19164
09-03 18:03:08.171   479   620 I ActivityManager: Displayed com.amazon.mShop.android/com.amazon.mShop.search.SearchActivity: +105ms
09-03 18:04:24.937   479   967 I ActivityManager: Starting: Intent { cmp=com.amazon.mShop.android/com.amazon.mShop.barcodeSearch.BarcodeFirstTryTipActivity } from pid 19164
09-03 18:04:25.046   479   620 I ActivityManager: Displayed com.amazon.mShop.android/com.amazon.mShop.barcodeSearch.BarcodeFirstTryTipActivity: +92ms
09-03 18:05:00.554   479  5949 I ActivityManager: Starting: Intent { flg=0x20000 cmp=com.amazon.mShop.android/com.amazon.mShop.search.SearchActivity (has extras) } from pid 19164
09-03 18:06:43.781   479  1019 I ActivityManager: Starting: Intent { act=intentActionStartActivity flg=0x4000000 cmp=com.amazon.mShop.android/com.amazon.mShop.home.HomeActivity } from pid 19164
09-03 18:07:12.984   479  1001 I ActivityManager: Starting: Intent { dat= flg=0x20000 cmp=com.amazon.mShop.android/com.amazon.mShop.search.SearchActivity (has extras) } from pid 19164
09-03 18:07:13.140   479   620 I ActivityManager: Displayed com.amazon.mShop.android/com.amazon.mShop.search.SearchActivity: +135ms
09-03 18:08:29.890   479  5949 I ActivityManager: Starting: Intent { cmp=com.amazon.mShop.android/com.amazon.mShop.barcodeSearch.BarcodeFirstTryTipActivity } from pid 19164
09-03 18:08:29.976   479   620 I ActivityManager: Displayed com.amazon.mShop.android/com.amazon.mShop.barcodeSearch.BarcodeFirstTryTipActivity: +81ms
09-03 18:09:05.515   479   961 I ActivityManager: Starting: Intent { flg=0x20000 cmp=com.amazon.mShop.android/com.amazon.mShop.search.SearchActivity (has extras) } from pid 19164
09-03 18:18:42.132   479  1039 I ActivityManager: Starting: Intent { act=intentActionStartActivity flg=0x20000 cmp=com.amazon.mShop.android/com.amazon.mShop.categoryBrowse.CategoryBrowseActivity } from pid 19164
09-03 18:18:42.359   479   620 I ActivityManager: Displayed com.amazon.mShop.android/com.amazon.mShop.categoryBrowse.CategoryBrowseActivity: +144ms
09-03 18:19:09.890   479  1030 I ActivityManager: Starting: Intent { act=intentActionStartActivity flg=0x4000000 cmp=com.amazon.mShop.android/com.amazon.mShop.home.HomeActivity } from pid 19164
09-03 18:19:17.406   479   485 I ActivityManager: Starting: Intent { dat= flg=0x20000 cmp=com.amazon.mShop.android/com.amazon.mShop.search.SearchActivity (has extras) } from pid 19164
09-03 18:19:17.515   479   620 I ActivityManager: Displayed com.amazon.mShop.android/com.amazon.mShop.search.SearchActivity: +97ms
09-03 18:20:58.500   479  5949 I ActivityManager: Starting: Intent { flg=0x20000 cmp=com.amazon.mShop.android/.search.image.SnapItActivity } from pid 19164
09-03 18:20:58.546   479   967 I ActivityManager: Starting: Intent { cmp=com.amazon.mShop.android/.search.image.SnapItPhotoCaptureActivity } from pid 19164
09-03 18:20:58.898   479   620 I ActivityManager: Displayed com.amazon.mShop.android/.search.image.SnapItPhotoCaptureActivity: +340ms (total +387ms)
09-03 18:21:26.156   479  1039 I ActivityManager: Starting: Intent { cmp=com.amazon.mShop.android/com.amazon.mShop.barcodeSearch.BarcodeFirstTryTipActivity } from pid 19164
09-03 18:21:26.359   479   620 I ActivityManager: Displayed com.amazon.mShop.android/com.amazon.mShop.barcodeSearch.BarcodeFirstTryTipActivity: +185ms
09-03 18:21:42.750   479   957 W ActivityManager: Duplicate finish request for HistoryRecord{408f9da0 com.amazon.mShop.android/com.amazon.mShop.barcodeSearch.BarcodeFirstTryTipActivity}
09-03 18:26:05.968   479   963 I ActivityManager: Starting: Intent { act=intentActionStartActivity flg=0x4000000 cmp=com.amazon.mShop.android/com.amazon.mShop.home.HomeActivity } from pid 19164
09-03 18:27:53.515   479   957 I ActivityManager: Starting: Intent { flg=0x10000000 cmp=com.amazon.mShop.android/com.amazon.mobileads.mraid.MraidBrowser (has extras) } from pid 19164
09-03 18:27:54.750   479   620 I ActivityManager: Displayed com.amazon.mShop.android/com.amazon.mobileads.mraid.MraidBrowser: +1s218ms
09-03 18:29:12.726   479   484 I ActivityManager: Starting: Intent { act=intentActionStartActivity flg=0x4000000 cmp=com.amazon.mShop.android/com.amazon.mShop.home.HomeActivity (has extras) } from pid 19164
09-03 18:29:15.609   479  1039 I ActivityManager: Starting: Intent { flg=0x10000000 cmp=com.amazon.mShop.android/com.amazon.mobileads.mraid.MraidBrowser (has extras) } from pid 19164
09-03 18:29:16.882   479   620 I ActivityManager: Displayed com.amazon.mShop.android/com.amazon.mobileads.mraid.MraidBrowser: +1s261ms
09-03 18:32:41.750   479   967 I ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.amazon.mShop.android/com.amazon.mShop.home.HomeActivity } from pid 1230

现在我想从文件中提取活动名称。它要从与此类似的每一行中提取,

09-03 18:29:15.609   479  1039 I ActivityManager: Starting: Intent { flg=0x10000000 cmp=com.amazon.mShop.android/com.amazon.mobileads.mraid.MraidBrowser (has extras) } from pid 19164

或者

09-03 17:12:08.601   479   620 I ActivityManager: Resumed Activity: com.amazon.mShop.android/com.amazon.mShop.search.SearchActivity totalTime: 4037 ms

从这一行我只需要字符串com.amazon.mobileads.mraid.MraidBrowser 需要删除该行的其他部分。不包含Starting:IntentActivityManager: Resumed Activity:的行应该被删除。

最后我会有一个字符串序列,比如,

com.amazon.mobileads.mraid.MraidBrowser

com.amazon.mShop.search.SearchActivity

依此类推,具体取决于他们的行号。

我怎样才能做到这一点?我可以使用字符串标记器,但这是一个乏味且效率低下的过程,我认为正则表达式匹配或类似的东西会更好。

4

2 回答 2

1

你可以试试正则表达式

ActivityManager: (?:Starting: Intent.*?cmp=|Resumed Activity:\s*)(\S+)

您想要的子字符串将存储在组 1中。

在 Regexr 上查看

要在 Java 中使用这个正则表达式,您必须转义所有反斜杠,所以它应该看起来像

ActivityManager: (?:Starting: Intent.*?cmp=|Resumed Activity:\\s*)(\\S+)

第一部分匹配来自“ActivityManager”的字符串,直到您要提取的部分,最后一部分(\\S+)匹配一系列非空白字符并将其存储在第 1 组中。

于 2012-09-04T08:15:14.737 回答
0

我知道我的方法可能一点也不可爱,但它确实有效。将是一种解决方案:

String filePath = "res/textfile.txt";
String line;
List<String> myL = new ArrayList<String>();

try {
    BufferedReader read = new BufferedReader(new FileReader(filePath)); 
    while((line = read.readLine()) != null) {
        if(line.contains("Resumed Activity") || line.contains("Starting: Intent")) {
            String str[] = line.split(" ");
            for(String s : str) {
                if(s.contains("com.amazon"))
                    myL.add(s);
            }
        }
    }

    for(String s : myL) {
         System.out.println(s);
    }

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

更新: 我刚刚再次阅读了您的帖子并更新了我的解决方案。我以某种方式错过了应该删除包括“Resumed Activity”或“Starting: Intent”在内的行。

于 2012-09-04T07:26:59.493 回答