Skip to main content
This example demonstrates how to use the app lock feature to keep automation within a specific app (WhatsApp in this case). The app lock ensures the agent stays in the messaging app even if the user accidentally navigates away.
This example is available on GitHub: app_lock_messaging.py

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

Clear field descriptions help the LLM extract accurate data.

2. Create Agent Configuration

Make sure your llm-config.defaults.jsonc includes the Contextor agent configuration for app lock support.

3. Build Task with App Lock

Key points:
  • with_locked_app_package() activates app lock with the WhatsApp package name
  • Use the full package name (Android: com.whatsapp or iOS: com.whatsapp)
  • Consider increasing max_steps for complex tasks with multiple interactions

4. Run Task and Handle Results

The result is typed as MessageResult | None for type safety.

Finding Your App Package Name

WhatsApp:
Other apps:

LLM Configuration

Make sure your llm-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 keys
4

Run the Script

Expected Output

How App Lock Works

When you enable app lock:
  1. Initial Launch: Agent verifies WhatsApp is open, launches it if needed
  2. Runtime Monitoring: Contextor agent monitors app changes at each step
  3. Smart Decisions: If user leaves WhatsApp, Contextor decides:
    • Allow the deviation (e.g., OAuth flows, system dialogs)
    • Relaunch the app (e.g., accidental navigation)
  4. 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:
The task will display a πŸ”’ indicator showing the locked package.

Execute via SDK

PlatformTaskRequest does not support overriding locked_app_package via SDK. To change the locked app, update the platform task configuration.

Execute via Cloud (No Code)

You can also run tasks with app lock directly from the platform UI:
  1. Go to Tasks
  2. Click Run on the task card (shows πŸ”’ indicator)
  3. Select Cloud Execution tab
  4. Choose LLM profile and cloud device
  5. Click Run Task
Platform app lock is ideal when you want consistent app restriction across all executions without code changes.

Customization Ideas

Troubleshooting

App Fails to Launch

Solutions:
  • 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