一. 描述
1. 执行monkey测试准备。
2. 离线执行monkey
【预期结果】:
正常进行测试无异常
【实际结果】:
monkey测试过程中频繁黑屏
【备注】:monkey 脚本:
adb shell "monkey -p com.android.dialer --throttle 1000 --ignore-crashes --ignore-timeouts --ignore-security-exceptions --ignore-native-crashes --monitor-native-crashes -v 100000 >> /sdcard/Stability_base.txt 2>> /sdcard/Stability_base_err.txt"
二.分析
1.每隔0.1s启动一次DataSharingActivity。
07-08 17:25:34.639 1276 3330 I ActivityTaskManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10000100 cmp=com.qualcomm.qti.watchhome/com.android.watchhome.main.DataSharingActivity} from uid 0
07-08 17:25:34.715 1276 1373 I ActivityTaskManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10000100 cmp=com.qualcomm.qti.watchhome/com.android.watchhome.main.DataSharingActivity} from uid 0
2. 查看日志,发现有很多Rejecting start
07-09 11:14:49.752 shell 4115 4131 I Monkey : // Rejecting start of Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER_APP] cmp=com.qualcomm.qti.watchhome/com.android.watchhome.main.MainActivity } in package com.qualcomm.qti.watchhome
三.查看代码
1. "Rejecting start" 打印来源
development/cmds/monkey/src/com/android/commands/monkey/Monkey.java
/** * Monitor operations happening in the system. */ private class ActivityController extends IActivityController.Stub { public boolean activityStarting(Intent intent, String pkg) { final boolean allow = isActivityStartingAllowed(intent, pkg); if (mVerbose > 0) { // StrictMode's disk checks end up catching this on // userdebug/eng builds due to PrintStream going to a // FileOutputStream in the end (perhaps only when // redirected to a file?) So we allow disk writes // around this region for the monkey to minimize // harmless dropbox uploads from monkeys. StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites(); Logger.out.println(" // " + (allow ? "Allowing" : "Rejecting") + " start of " + intent + " in package " + pkg); StrictMode.setThreadPolicy(savedPolicy); } currentPackage = pkg; currentIntent = intent; return allow; }
当不满足isActivityStartingAllowed 函数时,就会Rejecting,从代码逻辑上看,应该是有ACTION_MAIN,但是,没有包含CATEGORY_HOME,导致总是被Rejecting。
private boolean isActivityStartingAllowed(Intent intent, String pkg) { if (MonkeyUtils.getPackageFilter().checkEnteringPackage(pkg)) { return true; } if (DEBUG_ALLOW_ANY_STARTS != 0) { return true; } // In case the activity is launching home and the default launcher // package is disabled, allow anyway to prevent ANR (see b/38121026) final Set<String> categories = intent.getCategories(); if (intent.getAction() == Intent.ACTION_MAIN && categories != null && categories.contains(Intent.CATEGORY_HOME)) { try { final ResolveInfo resolveInfo = mPm.resolveIntent(intent, intent.getType(), 0, ActivityManager.getCurrentUser()); final String launcherPackage = resolveInfo.activityInfo.packageName; if (pkg.equals(launcherPackage)) { return true; } } catch (RemoteException e) { Logger.err.println("** Failed talking with package manager!"); return false; } } return false; }
2. 查看MainActivity 定义:
确实没有定义CATEGORY_HOME
45 <activity
46 android:name="com.android.watchhome.main.MainActivity"
47 android:label="@string/app_name"
48 android:clearTaskOnLaunch="true"
49 android:launchMode="singleInstance"
50 android:stateNotNeeded="true"
51 android:screenOrientation="nosensor"
52 android:theme="@style/LauncherTheme"
53 android:taskAffinity=""
54 android:enabled="true"
55 android:exported="true">
56
57 <intent-filter>
58 <action android:name="android.intent.action.MAIN" />
59 <category android:name="android.intent.category.DEFAULT" />
60 <category android:name="android.intent.category.LAUNCHER_APP"/>
61 </intent-filter>
62 </activity>
四. 方案:
移除单独创建的 DataSharingActivity, 在MainActivity 中申请CATEGORY_HOME