Welcome back to yet another amazing article. By the way, have you seen the new video uploaded at YT? This article is about the same. Now, If you think about why do I write this blog after creating a video - well, a video is mostly a quick take (for me). We miss many things. Writing a blog mostly explains everything.
Yes, now you have an idea about what I’m talking about. It’s about Termux-API and how we can make use of them in real life applications.
In that video, I’m specifically talking about termux-camera-photo API.
termux-am
termux-am-socket
termux-api-start
termux-api-stop
termux-audio-info
termux-backup
termux-battery-status
termux-brightness
termux-call-log
termux-camera-info
termux-camera-photo
termux-change-repo
termux-clipboard-get
termux-clipboard-set
termux-contact-list
termux-dialog
termux-download
termux-dyn-client
termux-fingerprint
termux-fix-shebang
termux-info
termux-infrared-frequencies
termux-infrared-transmit
termux-job-scheduler
termux-keystore
termux-location
termux-media-player
termux-media-scan
termux-microphone-record
termux-nfc
termux-notification
termux-notification-list
termux-notification-remove
termux-open
termux-open-url
termux-reload-settings
termux-reset
termux-restore
termux-sensor
termux-setup-package-manager
termux-setup-storage
termux-share
termux-sms-inbox
termux-sms-list
termux-sms-send
termux-speech-to-text
termux-storage-get
termux-telephony-call
termux-telephony-cellinfo
termux-telephony-deviceinfo
termux-toast
termux-torch
termux-tts-engines
termux-tts-speak
termux-usb
termux-vibrate
termux-volume
termux-wake-lock
termux-wake-unlock
termux-wallpaper
termux-wifi-connectioninfo
termux-wifi-enable
termux-wifi-scaninfo
Unfortunately, most are not listed in the API docs or anywhere. But what’s nice about these APIs is that they are made for everyone to use. You don’t need to know any internals of any of these. Termux gives a high-level API to interact with our hardware and Android API.
If you take a look at the camera API help, it says:
[-c camera-id] output-file where camera-id can be seen from termux-camera-info.
There is another interesting API which is termux-sms-[list,send,inbox] . When you list the inbox, you will get the output in JSON.
[
{
"threadid": 95,
"type": "inbox",
"read": true,
"sender": "Amal",
"number": "+91894xxxxx",
"received": "2022-06-18 19:17:38",
"body": "bruv! take scooter",
"_id": 2453
}
]
By default it will output the most recent messages. You can also limit the output with the -limit flag.
Now, how to utilise this API depends on you. But I could impress you to use it by saying — listen for incoming messages or set a cron job, on receiving check for a specific mobile number, check if the content hash matches what you’re looking for, if yes, nuke everything!
Or, what about building an online service or an application of your own that keeps in sync with your inbox? So you could read your messages without your phone in hand. Well, that’s just an alternative to Google’s stock Messages app.
termux-open-urlIs yet another useful API. Again, the awesomeness depends on how you use it. But, I’ve a video explaining the same.
An important API you should be playing with is the notification API. You could set a cron job or trigger it on any event to notify yourself. Termux can make use of Android’s notification API and so can your script. You can even use a custom notification sound, which can be changed from the app notification settings. Make your notification sound different from your normal phone notifications — so you’ll know when your server has gone down or crypto is falling and you’re going bankrupt in a few minutes.
termux-job-scheduler: Alternative to a cron job but you’re very limited about the interval of script execution. Basically, you are running some kind of script at the interval of 15 minutes. You cannot go less than that.
Note that since Android N, the minimum period is 900,000ms (15 minutes).
You should first start the API service by termux-api-start and stop whenever needed. You can test your API with below script:
#!/data/data/com.termux/files/usr/bin/python
import random
name=str(random.randint(1,20))
path="/data/data/com.termux/files/home/job"
with open(f"{path}/{name}.txt",'w') as f:
f.write(name)
termux-job-scheduler -s ~/scripts/RandScript --period-ms 900000
Check if the files are created in the folder mentioned in the path. If yes, you’re good to go. See the available pending jobs with termux-job-scheduler -p.
Other APIs are self explanatory and you can access the help page of any of these by appending -h flag after the name of API. Ofcourse, I’m not going to cover everything. But, I’ll append whatever new stuff that I’ve done here.
I would say, the APIs get interesting depending on your need. These APIs are just doors you can open and take whatever you need. As always, I’m just letting you know the possibilities. You as a programmer can tweak it in any way.
That’s all. Do let me know your suggestions & feedbacks via e-mail.
It’s Friday again. Have a better weekend.đź‘“