如何使用终端运行特定的Android应用程序? [重复]

时间:2022-02-10 00:12:58

This question already has an answer here:

这个问题在这里已有答案:

I installed Eclipse and Android SDK already. Everything is running fine.

我已经安装了Eclipse和Android SDK。一切都运行良好。

I want to install an .apk file so I follow the instruction already. But the problem is, when I start the emulator, it doesn't run my app automatically.

我想安装一个.apk文件,所以我已经按照说明操作了。但问题是,当我启动模拟器时,它不会自动运行我的应用程序。

Is there a command in the terminal that allow me to run the specific app that I ask for it?

终端中是否有命令允许我运行我要求的特定应用程序?

4 个解决方案

#1


69  

Use the cmd activity start-activity (or the alternative am start) command, which is a command-line interface to the ActivityManager. Use am to start activities as shown in this help:

使用cmd活动start-activity(或替代am start)命令,该命令是ActivityManager的命令行界面。使用am开始活动,如以下帮助所示:

$ adb shell am
usage: am [start|instrument]
       am start [-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>]
                [-c <CATEGORY> [-c <CATEGORY>] ...]
                [-e <EXTRA_KEY> <EXTRA_VALUE> [-e <EXTRA_KEY> <EXTRA_VALUE> ...]
                [-n <COMPONENT>] [-D] [<URI>]
       ...

For example, to start the Contacts application, and supposing you know only the package name but not the Activity, you can use

例如,要启动“联系人”应用程序,并假设您只知道程序包名称而不知道“活动”,则可以使用

$ pkg=com.google.android.contacts
$ comp=$(adb shell cmd package resolve-activity --brief -c android.intent.category.LAUNCHER $pkg | tail -1)
$ adb shell cmd activity start-activity $comp

or the alternative

或替代方案

$ adb shell am start -n $comp

See also http://www.kandroid.org/online-pdk/guide/instrumentation_testing.html (may be a copy of obsolete url : http://source.android.com/porting/instrumentation_testing.html ) for other details.

有关其他详细信息,另请参阅http://www.kandroid.org/online-pdk/guide/instrumentation_testing.html(可能是过时网址的副本:http://source.android.com/porting/instrumentation_testing.html)。

To terminate the application you can use

要终止您可以使用的应用程序

$ adb shell am kill com.google.android.contacts

or the more drastic

或者更激烈

$ adb shell am force-stop com.google.android.contacts

#2


6  

You can Start the android Service by this command.

您可以通过此命令启动Android服务。

adb shell am startservice -n packageName/.ServiceClass

adb shell am startservice -n packageName / .ServiceClass

#3


4  

I keep this build-and-run script handy, whenever I am working from command line:

每当我从命令行工作时,我都会保留这个构建并运行的脚本:

PACKAGE=com.examply.demo
ACTIVITY=.MainActivity
APK_LOCATION=app/build/outputs/apk/app-debug.apk
echo "Package: $PACKAGE"

echo "Building the project with tasks: $TASKS"
./gradlew $TASKS

echo "Uninstalling $PACKAGE"
adb uninstall $PACKAGE

echo "Installing $APK_LOCATION"
adb install $APK_LOCATION

echo "Starting $ACTIVITY"
adb shell am start -n $PACKAGE/$ACTIVITY

#4


2  

I used all the above answers and it was giving me errors so I tried

我使用了上述所有答案,它给了我错误,所以我尝试了

adb shell monkey -p com.yourpackage.name -c android.intent.category.LAUNCHER 1

adb shell monkey -p com.yourpackage.name -c android.intent.category.LAUNCHER 1

and it worked. One advantage is you dont have to specify your launcher activity if you use this command.

它工作。一个优点是,如果使用此命令,则无需指定启动器活动。

#1


69  

Use the cmd activity start-activity (or the alternative am start) command, which is a command-line interface to the ActivityManager. Use am to start activities as shown in this help:

使用cmd活动start-activity(或替代am start)命令,该命令是ActivityManager的命令行界面。使用am开始活动,如以下帮助所示:

$ adb shell am
usage: am [start|instrument]
       am start [-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>]
                [-c <CATEGORY> [-c <CATEGORY>] ...]
                [-e <EXTRA_KEY> <EXTRA_VALUE> [-e <EXTRA_KEY> <EXTRA_VALUE> ...]
                [-n <COMPONENT>] [-D] [<URI>]
       ...

For example, to start the Contacts application, and supposing you know only the package name but not the Activity, you can use

例如,要启动“联系人”应用程序,并假设您只知道程序包名称而不知道“活动”,则可以使用

$ pkg=com.google.android.contacts
$ comp=$(adb shell cmd package resolve-activity --brief -c android.intent.category.LAUNCHER $pkg | tail -1)
$ adb shell cmd activity start-activity $comp

or the alternative

或替代方案

$ adb shell am start -n $comp

See also http://www.kandroid.org/online-pdk/guide/instrumentation_testing.html (may be a copy of obsolete url : http://source.android.com/porting/instrumentation_testing.html ) for other details.

有关其他详细信息,另请参阅http://www.kandroid.org/online-pdk/guide/instrumentation_testing.html(可能是过时网址的副本:http://source.android.com/porting/instrumentation_testing.html)。

To terminate the application you can use

要终止您可以使用的应用程序

$ adb shell am kill com.google.android.contacts

or the more drastic

或者更激烈

$ adb shell am force-stop com.google.android.contacts

#2


6  

You can Start the android Service by this command.

您可以通过此命令启动Android服务。

adb shell am startservice -n packageName/.ServiceClass

adb shell am startservice -n packageName / .ServiceClass

#3


4  

I keep this build-and-run script handy, whenever I am working from command line:

每当我从命令行工作时,我都会保留这个构建并运行的脚本:

PACKAGE=com.examply.demo
ACTIVITY=.MainActivity
APK_LOCATION=app/build/outputs/apk/app-debug.apk
echo "Package: $PACKAGE"

echo "Building the project with tasks: $TASKS"
./gradlew $TASKS

echo "Uninstalling $PACKAGE"
adb uninstall $PACKAGE

echo "Installing $APK_LOCATION"
adb install $APK_LOCATION

echo "Starting $ACTIVITY"
adb shell am start -n $PACKAGE/$ACTIVITY

#4


2  

I used all the above answers and it was giving me errors so I tried

我使用了上述所有答案,它给了我错误,所以我尝试了

adb shell monkey -p com.yourpackage.name -c android.intent.category.LAUNCHER 1

adb shell monkey -p com.yourpackage.name -c android.intent.category.LAUNCHER 1

and it worked. One advantage is you dont have to specify your launcher activity if you use this command.

它工作。一个优点是,如果使用此命令,则无需指定启动器活动。