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

Common adaptation solutions for mobile terminals

I have been working on mobile pages for some time...

Vomiting blood sorting | 62 self-media platforms

With the rapid development of social media in rec...

How much does it cost to customize the Korla wood mini program?

The mini program provides convenience for publici...

Are email open rates plummeting and undervalued?

A friend recently told me an interesting phenomen...

Are electric toothbrushes a waste of money?

Recently, I saw a netizen saying that he has to h...

How to write a public relations event plan? You need to grasp these 11 points

With the development of the market economy, publi...

Where do Pinduoduo’s new users come from?

There is perhaps only one month left before Pindu...