import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.widget.EditText; import android.widget.Toast; public class SimpleName extends Activity { private EditText m_text = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Hello!"); builder.setMessage("What is your name?"); m_text = new EditText(this); builder.setView(mEditText);
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(SimpleName.this, "Hello World!", Toast.LENGTH_SHORT).show();
} });
...
import android
droid = android.Android()
name = droid.getInput("Hello!", "What is your name?")
droid.makeToast("Hello, %s" % name.result)
Python program: 4 SLOC
Any program that implements a compatible JSON–based RPC interfacing module or set of routines can potentially invoke the SL4A RPC Server.
https://github.com/damonkohler/sl4a/blob/master/python/ase/android.py
{ "Image": { "Width": 800, "Height": 600, "Title": "View from 15th Floor", "Thumbnail": { "Url": "http://www.example.com/image/481989943", "Height": 125, "Width": "100" }, "IDs": [116, 943, 234, 38793],
...
<script language= "javascript" type= "text/javascript">
var droid = new Android();
var date = new Date();
droid.eventPost("EVENT_A", "Page Loaded On " + date);
</script>
import android
droid = android.Android()
droid.webViewShow("/sdcard/sl4a/scripts/html/example.html")
result = droid.eventWaitFor("EVENT_A").result[“data”]
self.droid.log("Received data from EVENT_A: " + result)
import android
droid = android.Android()
message = droid.recognizeSpeech("Say something?")
droid.makeToast("You said: %s" % message.result)
import android
droid = android.Android()
droid.vibrate()
droid.ttsSpeak("Hello, this is android speaking!")
import android
droid = android.Android()
message = droid.webViewShow("http://xkcd.com/353/")
import android
droid = android.Android()
droid.smsSend(someNumber,"SMS Sent!")
import android, random
from fullscreenwrapper2 import *
class DemoLayout(Layout):
def __init__(self):
super(DemoLayout,self).__init__(xmldata,"FullScreenWrapper Demo")
def on_show(self):
self.add_event(key_EventHandler(handler_function=self.close_app))
self.views.but_change.add_event(click_EventHandler(self.views.but_change, self.change_color))
self.views.but_exit.add_event(click_EventHandler(self.views.but_exit, self.close_app))
self.views.txt_colorbox.background="#ffffffff"
def on_close(self):
pass
def close_app(self,view,event):
FullScreenWrapper2App.exit_FullScreenWrapper2App()
def change_color(self,view, event):
colorvalue = "#ff"+self.get_rand_hex_byte()+self.get_rand_hex_byte()+self.get_rand_hex_byte()
self.views.txt_colorbox.background=colorvalue
def get_rand_hex_byte(self):
j = random.randint(0,255)
hexrep = hex(j)[2:]
if(len(hexrep)==1):
hexrep = '0'+hexrep
return hexrep
if __name__ == '__main__':
droid = android.Android()
random.seed()
FullScreenWrapper2App.initialize(droid)
FullScreenWrapper2App.show_layout(DemoLayout())
FullScreenWrapper2App.eventloop()
Python project template
that will embed a Python interpreter
and your Python scripts into an APK.