What This Example Does
1
Launches WhatsApp
Verifies and launches WhatsApp before starting
2
Sends messages
Sends messages to multiple contacts while staying in the app
3
Enforces app lock
Automatically relaunches WhatsApp if accidentally closed
4
Returns results
Provides structured output of sent messages
Key Concepts
App Lock
Restricts execution to a specific app package/bundle ID
Automatic Relaunch
Agent relaunches app if user accidentally navigates away
Builder Pattern
Uses TaskRequestBuilder for advanced configuration
Structured Output
Returns typed results with Pydantic
Complete Code
app_lock_messaging.py
Code Breakdown
1. Define Output Structure
2. Create Agent Configuration
llm-config.defaults.jsonc includes the Contextor agent configuration for app lock support.
3. Build Task with App Lock
with_locked_app_package()activates app lock with the WhatsApp package name- Use the full package name (Android:
com.whatsappor iOS:com.whatsapp) - Consider increasing
max_stepsfor complex tasks with multiple interactions
4. Run Task and Handle Results
MessageResult | None for type safety.
Finding Your App Package Name
- Android
- iOS
WhatsApp:Other apps:
LLM Configuration
Make sure yourllm-config.defaults.jsonc includes Contextor configuration:
Running the Example
1
Ensure Prerequisites
- Device connected with USB debugging enabled
- WhatsApp installed and signed in
- ADB installed (Android) or idb installed (iOS)
- Python 3.12+
2
Install SDK
3
Configure LLM
Create
llm-config.defaults.jsonc and .env file with API keys4
Run the Script
Expected Output
How App Lock Works
When you enable app lock:- Initial Launch: Agent verifies WhatsApp is open, launches it if needed
- Runtime Monitoring: Contextor agent monitors app changes at each step
- Smart Decisions: If user leaves WhatsApp, Contextor decides:
- Allow the deviation (e.g., OAuth flows, system dialogs)
- Relaunch the app (e.g., accidental navigation)
- Completion: Task continues until goal is achieved
Platform App Lock
Instead of configuring app lock in code, you can configure it on the platform:Configure on Platform
When creating a task on platform.mobile-use.ai/tasks:Execute via SDK
Execute via Cloud (No Code)
You can also run tasks with app lock directly from the platform UI:- Go to Tasks
- Click Run on the task card (shows π indicator)
- Select Cloud Execution tab
- Choose LLM profile and cloud device
- Click Run Task
Customization Ideas
Lock to different app
Lock to different app
Send to many contacts
Send to many contacts
Handle with profile
Handle with profile
Troubleshooting
App Fails to Launch
- Verify WhatsApp is installed:
adb shell pm list packages | grep whatsapp - Ensure device is unlocked
- Check package name is correct
- Try launching manually:
adb shell monkey -p com.whatsapp 1
Agent Repeatedly Relaunches App
If the agent keeps relaunching WhatsApp:- Check agent thoughts to understand why itβs leaving
- The task may require actions outside WhatsApp (e.g., verification)
- Consider removing app lock for that specific action
Whatβs Next
Tasks and Task Requests
Learn more about task configuration options
Agent Profiles
Configure Contextor agent settings
Smart Notification Assistant
Explore more advanced examples

