Page 1 of 1

Android Shell Code

Posted: Sat Mar 14, 2020 4:12 pm
by thecoolpeople
Hello Programmers:

I have some cool code pieces, which can help many of you! All of these are for android, so have fun. Of course, there are many more, but I think, these are the important ones. If you have some more questions, please write them below! :awesome:
When you have some improvements, write me a private message, or below.

I will improve and find more of such cool commands, and I will share them with you!

Open file with the standard program: (by thecoolpeople)

Code: Select all

local openFile = function(path, mimetype)
  path = love.filesystem.getSaveDirectory() .. "/" .. path
  path = "file://" .. path
  os.execute("am start --user 0 -a android.intent.action.VIEW -d " .. path .. " -t " .. mimetype)
end
Share file: (by thecoolpeople)

Code: Select all

local shareFile = function(path, mimetype)
  path = love.filesystem.getSaveDirectory() .. "/" .. path
  path = "file://" .. path
  os.execute("am start --user 0 -a android.intent.action.SEND -t " .. mimetype .. " --eu android.intent.extra.STREAM " .. path)
end

Re: Android Shell Code

Posted: Thu Mar 26, 2020 12:58 am
by Kunologist
Woah, so happy to see that os.execute() is working on LOVE Android!
So I guess os.time() and os.date() would work too!

Re: Android Shell Code

Posted: Thu Mar 26, 2020 2:40 am
by AuahDark
Beware that code won't work in Android Nougat and later due to file:// URI banned in that and later version of Android. Of course all os.* functions work as they should, only limited by system permissions.

Re: Android Shell Code

Posted: Thu Mar 26, 2020 4:33 pm
by Kunologist
And I found a tiny bit more interesting below os.execute(). With this function we can make pur LOVE game write contents to external directories without root permission. The idea is the Linux behind every Android device.

If you want to create an external directory, use:

Code: Select all

os.execute("mkdir /sdcard/lovegame/savedata/")
(The path is just an instance of using mkdir. Avoid using /sdcard, it's a bad practice after all.)

And to write a file (if doesn't exist, will create it), use:

Code: Select all

os.execute([[echo 'Hello World!' >/sdcard/lovegame/savedata/helloworld.txt]])
And that's pretty amazing given that 11.3 or Android still left us with a backdoor to write external files.

Bad thing I haven't found out how to read, so bleh. Any ideas?

Re: Android Shell Code

Posted: Fri May 15, 2020 8:42 am
by Guard13007
Can you please explain why writing to /sdcard is considered bad practise and what we should use instead? Just stating not to do it without an alternative isn't very helpful. :(

Re: Android Shell Code

Posted: Sun Jul 17, 2022 6:16 am
by Kunologist
Guard13007 wrote: Fri May 15, 2020 8:42 am Can you please explain why writing to /sdcard is considered bad practise and what we should use instead? Just stating not to do it without an alternative isn't very helpful. :(
After so many years I finally came back to this topic again, sorry for inconveniences caused.

I've been working on PC lately, some of the knowledge is just in my head, so sorry if the information below lacks reference.

Firstly, not all Android devices have /sdcard or /mnt/sdcard/... . Android itself has system API for getting such prefixes, and it's a bad practice to presume that these directories exist.

Secondly, writing to external files may be bad as you may overwrite files that are generated by phone user. Who knows whether the user of the phone has /sdcard/lovegame/untitled/save.sav or not? You're potentially erasing the user's file!

Lastly, it's Android-platform-dependent whether this way of file-writing is supported or not. As we all know, people use all versions of Android, and you don't want your software to be only working on several specific Android versions.

Therefore, better take it as a backdoor instead of a good practice~