"""Offline Android LocalBroadcastManager isolation, force-offline workflow, and SD boot filter verifier."""
import json
VERSION = "CPython 3.12.14 offline Android component/IPC/Fragment verifier (Android SDK/emulator absent)"
def case_local_broadcast_manager_isolation():
# LocalBroadcastManager delivers strictly inside the same app process to dynamically registered receivers
app_a_dynamic = {"com.jay.mybcreceiver.LOGIN_OTHER": ["AppA.MyBcReceiver"]}
app_a_static = {"com.jay.mybcreceiver.LOGIN_OTHER": ["AppA.StaticReceiver"]}
app_b_dynamic = {"com.jay.mybcreceiver.LOGIN_OTHER": ["AppB.SnooperReceiver"]}
def send_local(action: str) -> list[str]:
# Static receivers and external apps NEVER receive local broadcasts
return list(app_a_dynamic.get(action, []))
actual = {
"delivered": send_local("com.jay.mybcreceiver.LOGIN_OTHER"),
"static_receivers_ignored": "AppA.StaticReceiver" not in send_local("com.jay.mybcreceiver.LOGIN_OTHER"),
"external_app_isolated": "AppB.SnooperReceiver" not in send_local("com.jay.mybcreceiver.LOGIN_OTHER"),
"modern_in_app_replacements": ["LiveData", "Kotlin SharedFlow"],
}
expected = {
"delivered": ["AppA.MyBcReceiver"],
"static_receivers_ignored": True,
"external_app_isolated": True,
"modern_in_app_replacements": ["LiveData", "Kotlin SharedFlow"],
}
return {
"name": "local_broadcast_manager_isolation",
"summary": "验证 LocalBroadcastManager 仅向本进程动态接收器投递、隔离静态注册与跨应用监听,并说明现代 LiveData/SharedFlow 替代方案。",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_force_offline_activity_collector_flow():
prefs = {}
activity_stack = []
events = []
def login(user: str, pawd: str) -> bool:
if user == "123" and pawd == "123":
prefs["user"] = user
prefs["pawd"] = pawd
activity_stack.append("MainActivity")
events.append("login_ok->MainActivity")
return True
events.append("login_failed")
return False
def trigger_force_offline():
events.append("dialog_shown:LOGIN_OTHER")
# Positive button: finishAll + start LoginActivity with FLAG_ACTIVITY_NEW_TASK
finished = list(activity_stack)
activity_stack.clear()
events.append(f"finishAll:{finished}")
activity_stack.append("LoginActivity(FLAG_ACTIVITY_NEW_TASK)")
events.append("relaunch:LoginActivity")
bad_ok = login("123", "wrong")
good_ok = login("123", "123")
activity_stack.append("ProfileActivity")
trigger_force_offline()
actual = {
"bad_login_ok": bad_ok,
"good_login_ok": good_ok,
"saved_user": prefs.get("user"),
"final_stack": activity_stack,
"events": events,
"api26_overlay_window_type": "TYPE_APPLICATION_OVERLAY",
}
expected = {
"bad_login_ok": False,
"good_login_ok": True,
"saved_user": "123",
"final_stack": ["LoginActivity(FLAG_ACTIVITY_NEW_TASK)"],
"events": [
"login_failed",
"login_ok->MainActivity",
"dialog_shown:LOGIN_OTHER",
"finishAll:['MainActivity', 'ProfileActivity']",
"relaunch:LoginActivity",
],
"api26_overlay_window_type": "TYPE_APPLICATION_OVERLAY",
}
return {
"name": "force_offline_activity_collector_flow",
"summary": "验证异地登录强制下线全流程:凭据校验、ActivityCollector 清空页面栈与 FLAG_ACTIVITY_NEW_TASK 重启登录页,并注明 API 26+ 浮窗类型演进。",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_sd_install_boot_split_filter_and_system_actions():
# Splitting BOOT_COMPLETED and MEDIA_MOUNTED into two separate <intent-filter> blocks avoids requiring scheme="file" for BOOT_COMPLETED
def match_filter(flt: dict, action: str, scheme: str | None) -> bool:
if action not in flt["actions"]:
return False
if flt["schemes"] and scheme not in flt["schemes"]:
return False
return True
merged_bad_filter = {
"actions": {"android.intent.action.BOOT_COMPLETED", "android.intent.action.MEDIA_MOUNTED"},
"schemes": {"file"},
}
split_filters = [
{"actions": {"android.intent.action.BOOT_COMPLETED"}, "schemes": set()},
{"actions": {"android.intent.action.MEDIA_MOUNTED", "android.intent.action.MEDIA_UNMOUNTED"}, "schemes": {"file"}},
]
dynamic_only_system_actions = [
"android.intent.action.BATTERY_CHANGED",
"android.intent.action.TIME_TICK",
"android.intent.action.SCREEN_ON",
"android.intent.action.SCREEN_OFF",
]
actual = {
"merged_matches_boot_without_scheme": match_filter(merged_bad_filter, "android.intent.action.BOOT_COMPLETED", None),
"split_matches_boot_without_scheme": any(match_filter(f, "android.intent.action.BOOT_COMPLETED", None) for f in split_filters),
"split_matches_media_mounted_file": any(match_filter(f, "android.intent.action.MEDIA_MOUNTED", "file") for f in split_filters),
"case_sensitive_uppercase_matches": any(match_filter(f, "ANDROID.INTENT.ACTION.MEDIA_MOUNTED", "file") for f in split_filters),
"dynamic_only_actions_count": len(dynamic_only_system_actions),
}
expected = {
"merged_matches_boot_without_scheme": False,
"split_matches_boot_without_scheme": True,
"split_matches_media_mounted_file": True,
"case_sensitive_uppercase_matches": False,
"dynamic_only_actions_count": 4,
}
return {
"name": "sd_install_boot_split_filter_and_system_actions",
"summary": "验证 SD 卡安装应用需拆分为两个 intent-filter(避免 data scheme 阻断 BOOT_COMPLETED)、Action 大小写敏感修正及仅限动态注册的系统广播。",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_local_broadcast_manager_isolation(),
case_force_offline_activity_collector_flow(),
case_sd_install_boot_split_filter_and_system_actions(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线 Android 组件/生命周期/IPC/Fragment 确定性验证环境(本地未安装 Android SDK/Gradle/ADB/模拟器)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线 Android 组件/生命周期/IPC/Fragment 确定性验证环境(本地未安装 Android SDK/Gradle/ADB/模拟器)。
入口文件:projects/android-tutorial-broadcastreceiver-2-lab/explore-tutorial-broadcastreceiver-2.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-tutorial-broadcastreceiver-2.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Android 组件/生命周期/IPC/Fragment 确定性验证环境(本地未安装 Android SDK/Gradle/ADB/模拟器)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联156章) · 含当前章节或实例 · mbskycom202612a1829.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1848.zip