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

Carbon Science | What is the grid emission factor in the dual-carbon economy?

The grid emission factor is a key parameter for e...

Which one is more important, conversion or traffic?

Why conversion is more important than traffic Fro...

How can operations move from the extensive stage to the refined stage?

Last week I went to Didi and was invited to talk a...

The first issue of Aiti Tribe: Spark offline analysis dimensions

【51CTO.com original article】 Activity description...

How far can Uber go in China?

No one can accurately predict the final outcome o...

Are the small fragments picked off from the teeth a sign of tooth damage?

gossip “Sometimes you can pick off small pieces o...

How can WeChat merchants survive after being “disdained” by friends?

The micro-businesses that were confused by the &q...