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

Can you make sales on Douyin explode? Use this formula!

I saw a TikTok video a few days ago. It showed st...

Can you believe it? Mosquitoes can actually help with vaccinations!

In order to enable the body to acquire immunity t...

What's the next step for smart home? Convincing consumers is the key

Do you use Nest to control your oven? Is your car...

A set of online event planning templates

As we enter 2022, the epidemic, which had been do...

An article reveals the "marketing decoding" of MINISO

How to build a good store brand? For example, fir...

New media operation: How to build your own internal and external matrix?

In this article, the author will talk to you abou...

6 information flow industry cases and delivery data, form costs exposed!

Today, Qingguajun will share with you the analysi...

Thailand passes bill to legalize same-sex marriage (full text)

Thailand's cabinet approves bill to legalize ...