Familja Java dhe J2ME


Select ‘Permissions’ and then ‘Add’ (last slide)



Yüklə 8,99 Mb.
səhifə8/8
tarix17.09.2018
ölçüsü8,99 Mb.
#68797
1   2   3   4   5   6   7   8

Select ‘Permissions’ and then ‘Add’ (last slide)

  • Select ‘Permissions’ and then ‘Add’ (last slide)

  • Select ‘Uses Permissions’ at this prompt

  • Set name to: android.permission.INTERNET and save (next slide)





All Android applications need to be signed

  • All Android applications need to be signed

    • The debug mode signs for you with special debug certificate
  • All MapView elements in map applications need to have an API key associated with them

    • That key must be registered with the certificate used to sign the app
  • When releasing app, need to sign with a release certificate and get a new API Key



For debug mode, get the MD5 fingerprint of the debug certificate

  • For debug mode, get the MD5 fingerprint of the debug certificate

    • Locate the ‘keystore’
      • Windows Vista: C:\Users\\.android\debug.keystore
      • Windows XP: C:\Documents and Settings\\.android\debug.keystore
      • OS X and Linux: ~/.android/debug.keystore
    • Use Keytool (comes with Java, in the bin directory with the other Java tools, should put that dir on system PATH) to get fingerprint
      • keytool -list –v -alias androiddebugkey -keystore “
        ” -storepass android -keypass android
        • If don’t include –v option, then will probably get only 1 fingerprint, and if it’s not MD5, then need –v (Java 7 needs –v)
      • Extract the MD5 fingerprint, SHA will not work unfortunately
  • Go to https://code.google. com/android/maps-api-signup.html , agree to terms and paste MD5 fingerprint, you will then be given an API Key



Need to put MapView tag in XML

  • Need to put MapView tag in XML

    • com.google.android.maps.MapView
    • MapView is the basic view that represents a Google Map display
    • Must include API Key in XML, inside a layout
    • android:id="@+id/mapview"
    • android:layout_width="fill_parent"
    • android:layout_height="fill_parent"
    • android:clickable="true"
    • android:apiKey=/>
  • Maps API Reference

    • http://code.google.com/android/add-ons/google-apis/reference/index.html


Android Developer’s Website

  • Android Developer’s Website

    • Activity and Service life-cycle flow charts
    • Tons of other Android info
  • Google Maps API external library

    • http://code.google.com/android/add-ons/google-apis/maps-overview.html
  • MightyPocket

    • http://www.mightypocket.com/2010/08/android-screenshots-screen-capture-screen-cast/
  • Numerous Forums & other developer sites, including:

    • http://www.javacodegeeks.com/2011/02/android-google-maps-tutorial.html
    • http://efreedom.com/Question/1-6070968/Google-Maps-Api-Directions
    • http://www.mail-archive.com/android-developers@googlegroups.com/msg28487.html
    • http://android.bigresource.com/ threads
    • http://groups.google.com/group/android-developers threads
    • Many http://stackoverflow.com threads
    • http://www.anddev.org/google_driving_directions_-_mapview_overlayed-t826.html
  • Zainan Victor Zhou – for advice and his own tutorial



The first mile on Android: relatively straight forward

  • The first mile on Android: relatively straight forward

    • Should already know Java
    • GUI is roughly drag and drop
    • Lots of documentation and examples online…
  • Let’s get a little more advanced…

    • Debugging on an Android device
    • Persistent application storage (Preferences, Database)
    • Writing and linking non-Java code (e.g., C and C++)
    • Cross-compiling useful libraries to Android
    • Modifying the kernel (e.g., to add support, remove permissions)
    • NFC support


As mentioned, we have a research project on Android which uses a lot of advanced features and different layouts

  • As mentioned, we have a research project on Android which uses a lot of advanced features and different layouts

    • Good examples of how to embed layouts within each other…
  • These layouts are available in our project:



Like “jailbreaking” -- a term most are familiar with

  • Like “jailbreaking” -- a term most are familiar with

    • Think of it as: it literally just gives you Linux root access
  • What don’t you need it for?

    • Writing a standard application that uses standard APIs and HW
    • Writing native code that does not touch some aspects of F.S.
  • What do you need it for?

    • Modifying and replacing the kernel
      • E.g., to remove permission checks, modify TCP, modify WiFi stack…
    • Loading and/or removing new kernel modules
    • Modifying anything in /system or snooping around /data
    • Modifying the Android OS or APIs
    • Changing the ROM on the device (new OS + kernel)


As usual, two ways to have debug information:

  • As usual, two ways to have debug information:

    • Android has a logging utility to print information to
    • Full debugger through ADB and Eclipse
  • Logging utility: good for printing periodic status of application and/or certain variables

  • Debugger: great for stepping through code, able to:

    • Set breakpoints and conditions
    • Check the values of any variable


You are likely going to want to persistently store data

  • You are likely going to want to persistently store data

  • In Android, there are 5 main ways to store data:

    • Shared Preferences – private key-value pair storage
    • Internal Storage – private storage per-application
    • External Storage – local shared storage (e.g., on SD card)
    • SQLite Database – private structured data
    • Network Storage – store data on your own server
  • You should be able to make a decision on which solution is best for various data in your application

    • Use multiple storage options!


Good for basic data storage, simple examples:

  • Good for basic data storage, simple examples:

    • Has the user completed the application settings? (boolean)
    • What is the user’s username? (string)
  • Shared Preferences accessed by string key, value can be:

    • boolean, float, int, long, string
  • Arbitrary objects cannot be stored in Shared Preferences, best two options for arbitrary objects:

    • Marshal to/from a private database (best)
    • Marshal to/from binary files in private storage


Name preference file (does not need to be globally unique)

  • Name preference file (does not need to be globally unique)

  • Get a handle to your preferences:

  • Can read in values by key, specify value if does not exist

  • Writing needs specific handle, and need to commit all changes



Should hopefully know the basics of databases

  • Should hopefully know the basics of databases

  • Best to store your main data/objects

    • Marshal your classes to and from a row in a database
  • Android provides some useful helper classes

    • SQLiteDatabase, Cursor, ContentValues….
  • Ultimately, I want to push you in the right direction, but you should be able to find the details…

    • Good example: http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/


JNI: framework that allows Java code to call, or be called by, native applications/libraries in other languages

  • JNI: framework that allows Java code to call, or be called by, native applications/libraries in other languages

    • E.g., Java code can call a function written in C++, and visa-versa
  • Easiest way to integrate pre-existing code and libraries in other languages (e.g., a pre-existing C encryption library)

  • Good to implement certain functionality which can be written more optimally in C, for example

  • Native code still runs within the application’s VM

    • Follows AndroidManifest (e.g., need “INTERNET” for sockets)
  • Android NDK: Toolset integrate/compile JNI in to Android app



Directory structure: native code fits in “jni” directory

  • Directory structure: native code fits in “jni” directory

  • ndk-build uses Android.mk in each native directory



Best way to make top-level Android.mk:

  • Best way to make top-level Android.mk:

  • You can cross-reference in-between libraries

    • E.g., stringlib can use a function found in mathlib, the stringlib Android.mk file just needs to specify it links mathlib


Each library needs its own Android.mk, with:

  • Each library needs its own Android.mk, with:

    • Local source files, linker/compiler flags, output (e.g., shared lib)
  • Something a little more advanced, for example:



Need to include JNI environment header, logging also useful

  • Need to include JNI environment header, logging also useful

  • Native function is passed …

    • Java environment: often used to allocate new Java objects
    • Java class object:
    • Your Parameters: any C/C++ primitives


Make sure ndk-build is in your PATH env. Variable

  • Make sure ndk-build is in your PATH env. Variable

    • Standard Android documentation should get you this far…
  • From the base of your application directory (outside jni dir):

  • All of your built libraries are copied to libs/armeabi/



YOU MUST REFRESH YOUR DIRECTORY IN ECLIPSE!!

  • YOU MUST REFRESH YOUR DIRECTORY IN ECLIPSE!!

    • Eclipse does not look for changes, even if you were to rebuild app
  • You must refresh every time you run ndk-build otherwise your changes will not be included

  • Maybe something built in to Eclipse now?



Must load each native library: e.g.,

  • Must load each native library: e.g.,

    • Can be done in any class or thread
    • It is a blocking call, to keep in mind if loading large libraries…
  • Must create native function prototypes in respective classes:

  • Once done, you can access these functions directly:



“I need a library to help me with _____, but it is a standard C or Linux implementation” (e.g., crypto library)

  • “I need a library to help me with _____, but it is a standard C or Linux implementation” (e.g., crypto library)

  • Proceed in this order:

    • Google “____ Android” – many libraries already cross-compiled
    • Write custom Android.mk to cross-compile it
  • You will be surprised how many libraries you find cross-compiled, and even if an older version: use their Android.mk



For many libraries, look at the Makefile and see which source files it uses to build the library

  • For many libraries, look at the Makefile and see which source files it uses to build the library

    • Add these sources to Android.mk “LOCAL_SRC_FILES”
    • Use similar CFLAGS, LDFLAGS
  • Common for libraries to link to pthread, remove –lpthread but keep most pthread code. Custom pthread force linked

  • Look at the many examples online, including our own:

    • libpcap, libnl, libgcrpyt, libglib, libgpg, libusb ….


Several key steps to modifying the kernel…

  • Several key steps to modifying the kernel…

    • Obtaining the proper source code for HW + OS version
      • If you get the wrong kernel: phone will not boot (I’ll help you…)
    • Modifying the kernel (we’ll give you some pointers…)
    • Cross-compiling the kernel using the NDK
    • Installing the kernel (BE CAREFUL: CAN BRICK PHONE)
      • Best to use something called “AnyKernel” package
  • This needs to be done in Linux (e.g., in a Virtual Machine)



The two possible phones you are using for this class:

  • The two possible phones you are using for this class:

    • Galaxy Nexus: code name “maguro” (“tuna” for kernel)
    • Galaxy S: code name “crespo”
  • Best to start at this link: https://android.googlesource.com/

  • Kernel source code depends on the HW, for you:

    • Galaxy Nexus: https://android.googlesource.com/kernel/omap.git
    • Nexus S: https://android.googlesource.com/kernel/samsung.git
  • In “About Phone” you can find your git kernel commit:



Build using cross-compiler provided by Google:

  • Build using cross-compiler provided by Google:

  • Setup your environment:

  • Checkout your kernel version: git checkout

  • Setup with the proper configuration

    • Galaxy Nexus: make tuna_defconfig
    • Nexus S: make crespo_defconfig
  • Finally, you can build with: make



Need to create a ramdisk with the the proper init scripts

  • Need to create a ramdisk with the the proper init scripts

    • Can be extracted from the device, but we will provide you with it
    • See example…
  • Modifications to ramdisk are needed if you want to:

    • Load kernel modules on boot
    • Set permissions to certain devices/files on boot
    • Change the order services are initialized


This is very tricky, and must be done properly or you will “brick” the phone, i.e., it will not boot and must be repaired

  • This is very tricky, and must be done properly or you will “brick” the phone, i.e., it will not boot and must be repaired

  • Install without building the entire OS by creating “update” ZIP

    • Runs a script to mount FS and extract kernel & modules
    • Usually includes system directory which is extracted
  • What the updater script looks like for Galaxy Nexus:



Example hierarchy of the update ZIP

  • Example hierarchy of the update ZIP



Need to use “recovery” mode on the phone to install

  • Need to use “recovery” mode on the phone to install

    • Dedicated, bootable partition with recovery console
  • Put your updater zip on your SD card, then:



Based on the project page, some kernel locations you might be interested in….

  • Based on the project page, some kernel locations you might be interested in….

    • WiFi Driver: drivers/net/wireless/bcmdhd
    • TCP Protocol / Sockets:
      • net/ipv4
      • include/net
    • Permissions (e.g., sockets): kernel/capability.c


Low rate short distance wireless communication

  • Low rate short distance wireless communication

    • Approx. 10 centimeters supported
    • 424 kbit/s across 13.56MHz
  • Current applications: payments, smart posters, check-ins…

  • Two major NFC modes:

    • Reader / Writer mode
    • Peer-to-Peer mode (emulate tags on Android)
  • Nexus S was first phone to really “mainstream” NFC



Standard data format created by NFC Forum

  • Standard data format created by NFC Forum

  • NDEF Messages: basic transportation mechanism

    • Can contain one or more NDEF Records
  • NDEF Record: contain specific structure with payload

    • TNF contains the record type (e.g., URI)


Start with basic tag read and write example

  • Start with basic tag read and write example

    • Includes P2P mode ability
  • This was an example posted by Google that we modified to make a little more intuitive

  • Sample code was posted for you:



As usual, you need to have Android permissions

  • As usual, you need to have Android permissions

    • Important to specify minimum SDK
    • Ensure that the phone has NFC support


Remember, all information in Android is broadcast

  • Remember, all information in Android is broadcast

  • For NFC, you register to receive TAG read broadcasts:



Specify that the Activity should receive NFC Intents:

  • Specify that the Activity should receive NFC Intents:

  • Can specify which data types the application should receive:

  • Need a handle to the “NFC Adapter” (i.e., hardware)

  • Apply and enable the filter in onResume()



Receive callbacks in “onNewIntent()” you override:

  • Receive callbacks in “onNewIntent()” you override:



NDEF Messages come in as an array

  • NDEF Messages come in as an array

  • Then, you can go through the msgs array and get the payload:



Just like you registered to receive NFC tag data, you register to know when a tag is simply detected and then use it to trigger a write to it:

  • Just like you registered to receive NFC tag data, you register to know when a tag is simply detected and then use it to trigger a write to it:

  • When the user specifies to write, you enable this filter and use a software flag to know that you should write when the tag is detected nearby:



Yüklə 8,99 Mb.

Dostları ilə paylaş:
1   2   3   4   5   6   7   8




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə