Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Android】プログラム上でadb shellコマンドを実行する方法

Posted at

プログラム上でadb shellコマンドを実行する方法

パッケージ名を指定して、別プロセスのアプリを終了させる場合

exec()の引数を変更することで様々なコマンドを実行できます

「adb」は省略してexec()に渡します

main.kt
class AdbService() {

    fun killProcess(packageName: String) {

        //adbから実行する場合は、「adb shell am force-stop com.google.android.apps.maps」となる
        val process = Runtime.getRuntime().exec("am force-stop $packageName")
        process.waitFor()

    }

    init {

        //GoogleMapを終了させる場合
        val googleMapPackageName = "com.google.android.apps.maps"
        killProcess(googleMapPackageName)

    }

}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
yuu_harada

@yuu_harada

Android Engineer

Comments

No comments

Let's comment your feelings that are more than good

Being held Article posting campaign

2025年、生成AIを使ってみてどうだった?

2026-01-19 ~ 2026-02-27

View details
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Login to continue?

Login or Sign up with social account

Login or Sign up with your email address