The powerful functions of Android terminal emulator Termux: application calls Termux to execute commands

The powerful functions of Android terminal emulator Termux: application calls Termux to execute commands

Introduction to Termux

Termux is an open source Android terminal emulator that uses apt and dpkg as package management tools to easily install, upgrade and manage various software packages.

Termux provides support for multiple programming languages, including Python, Ruby, Node.js, PHP, Perl, etc. After simple configuration, you can write and run code on your Android device. It also provides a wealth of command line tools, such as grep, awk, sed, and various text processing and network diagnostic tools.

Termux allows users to access and manage files on Android devices, including internal and external storage. It also includes an SSH client and server, allowing users to remotely connect to other computers or have other computers connect to Termux. In addition, it also supports version control systems such as Git and Subversion, and users can perform version control operations on their devices.

Call Termux to execute commands

Since Termux version 0.95, third-party applications can run commands in the context of a Termux application by sending an intent to RunCommandService or becoming a plugin for the termux-tasker plugin client.

  1. Configure Termux to allow external applications to call: Open Termux, modify the ~/.termux/termux.properties file, and add a line:
 allow-external-apps = true

For Android versions >= 10.0, you need to enable associated application permissions for Termux, otherwise third-party applications will not be allowed to directly start Termux in the background, but can only be called when Termux is running in the background.

Enable the "Associated Apps" permission: [App information] -> [Apps] -> [Termux] -> [Permissions] -> [Associated Apps].

  1. Application configuration: Request com.termux.permission.RUN_COMMAND permission in the AndroidManifest.xml file and send an Intent to RunCommandService in the code.
 <uses-permission android:name="com.termux.permission.RUN_COMMAND"/>

Intent construction in the code:

 intent.setClassName("com.termux", "com.termux.app.RunCommandService"); intent.setAction("com.termux.RUN_COMMAND"); intent.putExtra("com.termux.RUN_COMMAND_PATH", "/data/data/com.termux/files/usr/bin/top"); intent.putExtra("com.termux.RUN_COMMAND_ARGUMENTS", new String[]{"-n", "5"}); intent.putExtra("com.termux.RUN_COMMAND_WORKDIR", "/data/data/com.termux/files/home"); intent.putExtra("com.termux.RUN_COMMAND_BACKGROUND", false); intent.putExtra("com.termux.RUN_COMMAND_SESSION_ACTION", "0"); startService(intent);

Related parameter description:

  • com.termux.RUN_COMMAND_PATH: the ELF file called;
  • com.termux.RUN_COMMAND_ARGUMENTS: call command parameters;
  • com.termux.RUN_COMMAND_WORKDIR: working directory;
  • com.termux.RUN_COMAND_BACKGROUND: Whether to run in the background. If true, TermuxActivity will not be entered;
  • com.termux.RUN_COMMAND_SESSION_ACTION: Session action.

The top command is called here: /data/data/com.termux/files/usr/bin/top

<<:  Key APIs and techniques for adjusting Android brightness to achieve personalized APP brightness settings

>>:  Exploration and practice of Ctrip Hotel's unified cloud mobile phone platform

Recommend

Beiyu Reading and Writing Workshop Zhang Xingdong's Writing Class

Beiyu Reading and Writing Workshop Zhang Xingdong...

Refined operations: using RFM to segment users

The RFM model is not difficult to use. It is a mo...

How to improve the effect of information flow and double the conversion?

We all know that whether it is bidding or informa...

AiKop | See the 72 changes of modern ships

Do you know the origin of the name "cruise s...