このトピックでは、AgentBay SDK を使用したクラウドフォン向けアプリケーション管理機能について説明します。クラウド環境でクラウドフォンアプリケーションを起動、監視、制御する方法を扱います。
概要
Mobile Use モジュールは、Android モバイルデバイス向けにアプリケーション管理機能を提供します。これらの機能には、以下が含まれます。
アプリケーション検出 — デバイスにインストールされているアプリケーションのリストを照会します。
アプリケーションライフサイクル管理 — パッケージ名を使用してモバイルアプリケーションを起動および停止します。
アクティビティ管理 — 特定の Android アクティビティを起動します。
プロセス監視 — 実行中のアプリケーションとそれに関連するプロセスを追跡します。
セッションの作成
import os
from agentbay import AgentBay
from agentbay.session_params import CreateSessionParams
api_key = os.getenv("AGENTBAY_API_KEY")
if not api_key:
raise ValueError("AGENTBAY_API_KEY environment variable is required")
agent_bay = AgentBay(api_key=api_key)
params = CreateSessionParams(image_id="mobile_latest")
result = agent_bay.create(params)
if result.success:
session = result.session
print(f"Session created: {session.session_id}")
else:
print(f"Failed to create session: {result.error_message}")
exit(1)
# Actual output:
# Session created: session-04bdw8o39c9uiwet4
インストール済みアプリケーションの取得
モバイルデバイスにインストールされているアプリケーションのリストを照会します。
result = session.mobile.get_installed_apps(
start_menu=True,
desktop=False,
ignore_system_apps=True
)
if result.success:
apps = result.data
print(f"Found {len(apps)} installed applications")
for app in apps[:5]:
print(f"Name: {app.name}")
print(f"Start command: {app.start_cmd}")
print(f"Stop command: {app.stop_cmd if app.stop_cmd else 'N/A'}")
print(f"Working directory: {app.work_directory if app.work_directory else 'N/A'}")
print("---")
else:
print(f"Error: {result.error_message}")
# Actual output (using current mobile_latest image):
# Found 0 installed applications
パラメーター:
start_menu(bool):スタートメニューのアプリケーションを含めるかどうか。desktop(bool):デスクトップアプリケーションを含めるかどうか。ignore_system_apps(bool):システムアプリケーションを除外するかどうか。
戻り値:
InstalledAppListResultには、InstalledAppオブジェクトのリストが含まれます。
現在の mobile_latest イメージには、プリインストール済みアプリケーションがリストに含まれていないため、このメソッドは空のリストを返します。ただし、パッケージ名を使用してアプリケーションを起動することはできます。
アプリケーションの起動
monkey -p <package_name> コマンド形式を使用してモバイルアプリケーションを起動します。
パッケージ名による起動
# Launch an Android application using the "monkey -p" format
start_cmd = "monkey -p com.android.settings"
result = session.mobile.start_app(start_cmd)
if result.success:
processes = result.data
print(f"Application launched, {len(processes)} processes found")
for process in processes:
print(f"Process: {process.pname} (PID: {process.pid})")
else:
print(f"Failed to launch application: {result.error_message}")
# Actual output:
# Application launched, 1 process found
# Process: com.android.settings (PID: 2805)
アプリケーションを起動するには、常に
monkey -p <package_name>形式を使用してください。一般的な Android パッケージ名:
設定:
com.android.settingsChrome ブラウザ:
com.android.chrome電卓:
com.android.calculator2連絡先:
com.android.contacts
特定のアクティビティの起動 (Android)
start_cmd = "monkey -p com.android.settings"
activity = ".Settings"
result = session.mobile.start_app(
start_cmd=start_cmd,
activity=activity
)
if result.success:
processes = result.data
print(f"Application launched, Activity is {activity}")
print(f"Found {len(processes)} processes")
for process in processes:
print(f"Process: {process.pname} (PID: {process.pid})")
else:
print(f"Failed to launch application: {result.error_message}")
# Actual output:
# Application launched, Activity is .Settings
# Found 1 process
# Process: com.android.settings (PID: 2921)
activity パラメーターを使用すると、アプリケーション内で特定の活動を起動できます。例えば、以下のような形式です。
相対名:
.SettingsActivity完全名:
com.package/.Activity
一般的なアクティビティの例:
設定:
com.android.settingsとアクティビティ.Settings。ブラウザ:
com.android.chromeとアクティビティcom.google.android.apps.chrome.Main。電卓:
com.android.calculator2とアクティビティ.Calculator。
アプリケーションの停止
stop_cmd パラメーターを com.android.settings のようなパッケージ名に設定してください。
# Launch an application
start_result = session.mobile.start_app("monkey -p com.android.settings")
if start_result.success:
print("Application launched successfully")
for process in start_result.data:
print(f" Process: {process.pname} (PID: {process.pid})")
# Stop the application using its package name
result = session.mobile.stop_app_by_cmd("com.android.settings")
if result.success:
print("Application stopped successfully")
else:
print(f"Failed to stop application: {result.error_message}")
# Actual output:
# Application launched successfully
# Process: com.android.settings (PID: 3042)
# Application stopped successfully
完全なワークフローの例
import os
import time
from agentbay import AgentBay
from agentbay.session_params import CreateSessionParams
api_key = os.getenv("AGENTBAY_API_KEY")
if not api_key:
raise ValueError("AGENTBAY_API_KEY environment variable is required")
agent_bay = AgentBay(api_key=api_key)
# Create a mobile session
params = CreateSessionParams(image_id="mobile_latest")
result = agent_bay.create(params)
if not result.success:
print(f"Failed to create session: {result.error_message}")
exit(1)
session = result.session
print(f"Session created: {session.session_id}")
# Step 1: Launch application
print("Step 1: Launch Settings application...")
start_result = session.mobile.start_app("monkey -p com.android.settings")
if not start_result.success:
print(f"Failed to launch application: {start_result.error_message}")
agent_bay.delete(session)
exit(1)
print(f"Application launched, {len(start_result.data)} processes found")
for process in start_result.data:
print(f" - {process.pname} (PID: {process.pid})")
# Step 2: Wait for application to load
print("Step 2: Wait for application to load...")
time.sleep(3)
# Step 3: Stop application
print("Step 3: Stop application...")
stop_result = session.mobile.stop_app_by_cmd("com.android.settings")
if stop_result.success:
print("Application stopped successfully")
else:
print(f"Failed to stop application: {stop_result.error_message}")
# Clean up
print("Cleaning up session...")
agent_bay.delete(session)
print("Workflow complete!")
# Actual output:
# Session created: session-04bdwfj7tnhfnzibx
# Step 1: Launch Settings application...
# Application launched, 1 process found
# - com.android.settings (PID: 3268)
# Step 2: Wait for application to load...
# Step 3: Stop application...
# Application stopped successfully
# Cleaning up session...
# Workflow complete
API リファレンス
メソッド | パラメーター | 戻り値 | 説明 |
|
|
| インストール済みアプリケーションのリストを取得 |
|
|
| モバイルアプリケーションを起動 |
|
|
| パッケージ名でアプリケーションを停止 |
戻り値の型
InstalledAppListResult
success(bool):操作が成功したかどうか。data(List[InstalledApp]):インストール済みアプリケーションのリスト。error_message(str):操作が失敗した場合のエラーメッセージ。request_id(str):一意のリクエスト識別子。
InstalledApp
name(str):アプリケーション名。start_cmd(str):アプリケーションの起動に使用されるコマンドまたはパッケージ名。stop_cmd(Optional[str]):アプリケーションの停止に使用されるコマンド。work_directory(Optional[str]):アプリケーションの作業ディレクトリ。
ProcessListResult
success(bool):操作が成功したかどうか。data(List[Process]):プロセスオブジェクトのリスト。error_message(str):操作が失敗した場合のエラーメッセージ。request_id(str):一意のリクエスト識別子。
Process
pname(str):プロセス名。pid(int):プロセス ID。cmdline(Optional[str]):完全なコマンドライン。
AppOperationResult
success(bool):操作が成功したかどうか。error_message(str):操作が失敗した場合のエラーメッセージ。request_id(str):一意のリクエスト識別子。
モバイル固有のパラメーター
起動コマンドの形式
start_cmd パラメーターは「monkey -p」形式を使用する必要があります。
session.mobile.start_app("monkey -p com.android.settings")停止コマンドの形式
stop_cmd パラメーターをパッケージ名に設定してください。
session.mobile.stop_app_by_cmd("com.android.settings")アクティビティパラメーター (Android)
activity パラメーターを使用すると、特定の活動を起動できます。
session.mobile.start_app(
start_cmd="monkey -p com.android.settings",
activity=".Settings"
)アクティビティ名は次のように指定できます。
相対名:
.SettingsActivity。パッケージプレフィックスは自動的に追加されます。完全名:
com.package/.Activity。完全なアクティビティ識別子。