Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

PHP for Android: prototyping Android apps in php

PFA (PHP for Android) is a project that lets you write native Android apps in php. In this talk you'll learn how to install and use this project to develop on your phone with php.

  • Login to see the comments

PHP for Android: prototyping Android apps in php

  1. 1. PHP for Android Prototyping Android apps in php Cesare D'Amico
  2. 2. Cesare D'Amico freelance php dev Hi, nice to meet you! Twitter: __ce (double underscore)
  3. 3. What happens adding... +
  4. 4. Genetically modified php :)
  5. 5. We're going to talk about... + PFAf SL4A
  6. 6. Android
  7. 7. Android • built on top of a linux kernel • native libc (Bionic): 0.5 * sizeof(GNU libC) • runtime (Dalvik VM) + system libs • application framework • Java!
  8. 8. How can we make php run on Android? 1. static compilation with Glibc (over 7MB)... fat! 2. linked to Bionic: smaller (~2MB), but tweaks needed
  9. 9. How can we make php run on Android? 3. SL4A to the rescue!
  10. 10. SL4A – scripting layer for Android • Google project, led by Damon Kohler • Open Source • exposes Android API • quite active
  11. 11. SL4A: installation As easy as: 1) enable “Unknown sources” under Applications settings 2)
  12. 12. PHP for Android project • led by Iván Mosquera, software engineer at Irontec (the people behind IronPython and other OS projects) • Open Source • Started in 2009 • php-for-android on Google Groups
  13. 13. PFA: installation The PFA apk just installs other components: • – php binary • – scripts and PFAf • • Android.php + php.ini
  14. 14. Everything's installed!
  15. 15. SL4A: how does it work? Provides Android facades Makes the Android API available through JSON RPC calls
  16. 16. PFA: let's try it
  17. 17. PFA: let's try it <?php require_once('Android.php'); $droid = new Android(); $droid­>vibrate();
  18. 18. PFA: let's try it SOOOO easy! :-)
  19. 19. PFA: let's try it Let's try something else:
  20. 20. PFA: let's try it <?php require_once('Android.php'); $droid = new Android(); $name = $droid­>getInput('Hi!',   'What is your name?'); $droid­>makeToast('Hello '.   $name['result']);
  21. 21. PFA: let's try it
  22. 22. PFA: access phone features
  23. 23. PFA: access phone features <?php require_once('Android.php'); $droid = new Android(); $droid­>viewContacts();
  24. 24. PFA: access phone features Many more features available; API full list: Examples: smsSend, sendEmail, cameraCapturePicture, viewHTML, bluetoothConnect, contactsGet, viewMap... (perhaps not *everything* available yet)
  25. 25. PFA: writing scripts Directly on the phone!
  26. 26. PFA: writing scripts SOOOOO slow and unusable! :-(
  27. 27. PFA: writing scripts Including local files: <?php include('/sdcard/proj/...'); ?>
  28. 28. PFA: writing scripts allow_url_fopen + allow_url_include FTW!
  29. 29. PFA: including remote code <?php require_once('Android.php'); $droid = new Android(); include(' droid.php.txt'); <?php $droid­>makeToast("There's a  starman waiting in the sky!"); remote.php droid.php.txt
  30. 30. PFA: including remote code
  31. 31. PFA: including remote code include(' droid.php.txt'); Secure Handy ? This is here for testing purposes ONLY!
  32. 32. PFA: including remote code Don't forget that PFA, although yet useable in many respects, is still alpha software. In the future we'll be able to distribute scripts in apk format, without using allow_url_fopen
  33. 33. PFA: internals Android.php: <?php public function __call($name, $args){   return $this­>rpc($name, $args); } public function rpc($method, $args){   $data = array('id'=>$this­>_id, 'method'=>$method,                 'params'=>$args);   $request = json_encode($data)."n";   $sent = socket_write($this­>_socket,$request,                        strlen($request));
  34. 34. PFA: internals <?php public function rpc($method, $args){   $data = array('id'=>$this­>_id, 'method'=>$method,                 'params'=>$args);   $request = json_encode($data)."n";   $sent = socket_write($this­>_socket, $request,                       strlen($request));   $response = socket_read($this­>_socket, 1024,     PHP_NORMAL_READ)or die("Could not read inputn");   $this­>_id++;   $result = json_decode($response);   return array(     'id'=>$result['id'], 'result'=>$result['result'],     'error'=>$result['error']   ); }
  35. 35. PFAf: PFA framework Base php class: ScriptAbstract; apps extend this class and implement init() States-based: put on a stack the next action you want to execute function currentAction(){   ...   $this­>setNextAction('next'); } function nextAction() { ... }
  36. 36. Thanks everyone! ?
  37. 37. PFA wants you! You can help by writing scripts and sharing them: ...and there are good examples there! Take a look at the wi-fi scanner and the web server ;)
  38. 38. 12-14 Maggio 2011
  39. 39. Please, rate this talk! Feel like bashing? Urge saying you fell in love with this presentation? Now you can!