Hiển thị các bài đăng có nhãn Androi. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn Androi. Hiển thị tất cả bài đăng

12/2/13

Tạo và chạy ứng dụng Android đầu tiên với Eclipse


Tạo và chạy ứng dụng Android đầu tiên với Eclipse

Ở bài số 1 chúng ta đã học cách cài đặt và cấu hình môi trường lập trình Android trên IDE Eclipse. Các bạn có thể tham khảo bài trước ở đây
Bước 1: Chọn New Android App Project , xuất hiện hộp thoại sau:
Build SDK: mặc định là bản latest SDK bạn đã cái trên máy
MinimumRequired SDK: bản thấp nhất của Android hỗ trợ cho app của bạn

Bước 2: Tạo icon cho ứng dụng
Tham khảo http://developer.android.com/design/style/iconography.html
Bước 3: Chọn template cho project, với VD này là Blank Activity
Bước 4: Chọn next ở màn hình tiếp theo, nếu gặp màn hình này chọn Install/Upgrade
Chọn “Accept” và “Install”
Sau khi upgrade xong , ấn “Finish” và khởi động lại Eclipse
Một vài chú ý trước khi chạy project:
AndroidManifest.xml mô tả những đặc tính cơ bản và xác định các thành phần của ứng dụng. Bạn sẽ được học rõ hơn trong những bài tiếp theo.
Src: folder chính để lưu giữ các fie mã nguồn. Mặc định nó bao gồm lớp Activity sẽ được gọi khi khởi chạy ứng dụng
Res: chứa những thư mục con để lưu trữ hình ảnh âm thanh của ứng dụng
drawable-hdpi/
Thư mục cho các đối tượng drawable( chẳng hạn như ảnh bitmap) được thiết kế mật độ cao (hdpi). Các thư mục drawable khác chứa tài sản được thiết kế cho các mật độ màn hình khác.
layout/
Thư mục định nghĩa giao diện người dùng cho ứng dụng của bạn
values/
Thư mục cho các tập tin XML để định nghĩa màu sắc và chuỗi ký tự.
Trước khi chạy chúng ta phải thiết lập máy ảo như sau:
Chọn biểu tượng ở thanh toolbar, hộp thoại hiện lên chọn New, sau đó nhập một vài thông số:
Name: Tùy ý
Targert:  Chọn phiên bản hệ điều hành bạn muốn
SD Card: gõ size của SD Card ( mình để là 256 MiB)
Skin: Có thể để default (HVGA) hoặc chọn kích cỡ bạn muốn
Sau khi khởi tạo máy ảo chọn máy ảo và nhấn nút Start ở thanh công cụ bên phải. Giao diện sau khi chạy thành công

Quay lại với project MyFirstApp đã tạo ở bước trên, chọn và click Run từ thanh toolbar
Một số chức năng quan trọng cần biết khi lập trình Android
Android có một hệ thống debug hoàn hảo được hỗ trợ bởi logcat. Logcat sẽ thông báo toàn bộ về hệ điều hành, giúp chúng ta biết được  hệ điều hành đang làm gì, gọi đến cái gì, khởi chạy những gì, chiếm dụng bộ nhớ ra sao … (các bạn sẽ được nói rõ hơn trong những bài sau)

File Explorer của Android là tính năng hữu ích của Google đưa vào giúp chúng ta quản lý file trong sd card  và cả file system data. File Explorer giúp bạn dễ dàng đưa file vào/ lấy file ra trong sdcard ảo của simulator, xóa cơ sở dữ liệu để khởi tạo lại (only emulator)
Mở File Explorer bằng cách: Window->ShowView->Other->Android->File Explorer

13/12/12

Install Androidx86 in a VMware Workstation VM


Install Androidx86 in a VMware Workstation VM

I was inspired to try a full install of the Androidx86 OS in a VMware Workstation VM after Eric Sloof created a video of the Androidx86 LiveCD running in a vSphere VM. I’ve documented building the VM and configuring it to run in this post. I also list some navigation tips for getting around inside Androidx86. Finally check out my video of the working VM.
Since most are probably wondering I’ll mention this now, once the VM is connected to the Internet you can download a very limited selection of Apps, but I had sporadic success with the various Apps I tried. For the few that I did get to install, I wasn’t able to get all of them running. So, use an Androidx86 VM as a way to get a feel for what an Android device is like. Maybe it will help you make a decision about buying a Tablet like the Samsung Galaxy Tab.
Browsing the web worked great. I do not test email.

Download, Create New VM, and Install Androidx86 from .ISO

Download the latest revision of the Androidx86 .ISO here:
Build a new “typical” VMware Workstation VM (this should work just the same with the free VMware Player), and when you point the install wizard at the Androidx86 .iso the OS type FreeBSD is auto detected. I chose to install the operating system later as I did not want to invoke the auto download of the VMware Tools.
image image 
This choice forces you to manually specify FreeBSD from the next screen of the wizard.
image
I kept the default disk size of 8GB and to chose to store the virtual disk as a single file.
I customized the virtual hardware and did the following
  • bumped the RAM to 512 mb. The Workstation wizard suggested 256 mb.
  • deleted the floppy
  • pointed the vCD/DVD at the Androidx86 .iso and made sure it was connected at power on.
  • changed the vNIC to Bridged. NAT should work just fine too. (Note that additional networking changes need to be made later)
Once your ready, boot your new VM and choose to install to harddisk when prompted by the Android Installer.
image
Keep all installation defaults. The only hands on part is creating the partition on the hard disk. The steps are:
  • Create/Modify Partition when prompted
  • create new partition from free space
  • make it a primary partition and use all 8gb
  • make it bootable
  • write the changes
  • choose the new sda1 partition to install
  • format as ext3 when prompted
  • install GRUB when prompted
I also want to point out I you do not have to install VMware Tools at all, and I never even tried.
image

Configure Networking

After my install was finished and the VM was running for the first time, I could not get an ip address from my DHCP server. When I went to troubleshoot I discovered that there wasn’t an ethernet interface the OS could use. Sloof ran into this problem with his vSphere VM too, and in his video he demonstrates changing the adapter type to “flexible”. Since VMware Workstation does not give you an option to change adapter types in it’s GUI, I had to shutdown the VM and follow these instructions:
  1. Boot the LiveAndroid VM
  2. Hit Alt-F1 to switch to the root console.
  3. mkdir /data/misc/dhcp
  4. Stop the VM, and "Close" the VM in VMware Workstation (or Exit VMware Workstation)
  5. Edit your VM’s .vmx file so the "ethernet0.virtualDev" line reads: ethernet0.virtualDev = "vlance"
  6. Open the .vmx file (launch the VM in VMware)
  7. Set the VM to have at least 384MB RAM
  8. Start the VM; you’re networked.
BTW, other than hard powering off the Android VM, you can do a “guest shutdown” (even though Tools are not installed) from the Workstation Menu. Androidx86 powers off quick, and I am not sure if it’s graceful or not.
Also, the /data/misc/dhcp directory already existed in my VM, so step 3 above was not necessary. That was all I need to get a working networking connection.
Here’s a quick video of the Android OS in action. Sorry, but I didn’t bother editing first.
I found some Tips for getting around inside the VM at http://code.google.com/p/live-android/wiki/howtouse, but I’m adding to these with my own recommendations:

Androidx86 VM Navigation Tips

  • use alt+F1 or alt+F7 to switch GUI and console.
  • Use the HOME Key to return to the main desktop
  • Use ESC key as the BACK button
  • Use the right mouse key for the MENU key
  • Use right and left arrow keys to simulate swiping to new desktops
  • Left Click and hold the mouse to simulate a “long touch” and drag icons/widgets to other desktops or the trash icon, or to pull down the task bar.

12/12/12

Setting Up An Android App Build Environment With Eclipse, Android SDK, PhoneGap (Ubuntu 10.10) - Phan 2


7 Creating An Android App

To build my Android app (which I call TweetMe) from its HTML, CSS, and JavaScript sources, I create a folder calledtweetme in my home directory, and in that tweetme folder I place another folder called tweetme wich contains the sources (HTML, CSS, JavaScript):
HOME
    |
    |
    + tweetme
             |
             |
             +tweetme
                     |
                     |
                     SOURCES (HTML, CSS, JavaScript)
Go to ~/tweetme/tweetme/:
cd ~/tweetme/tweetme/
You should see the HTML, CSS, JavaScript sources in that directory:
ls -l
falko@falko-desktop:~/tweetme/tweetme$ ls -l
total 96
-rw-r--r-- 1 falko falko  4233 2011-01-24 00:20 index.html
drwxr-xr-x 2 falko falko  4096 2011-01-24 15:55 jqtouch
-rw-r--r-- 1 falko falko 78601 2011-01-23 23:15 jquery.js
drwxr-xr-x 4 falko falko  4096 2011-01-24 15:55 themes
falko@falko-desktop:~/tweetme/tweetme$
Now we can create an app either from the command line or by using Eclipse:

7.1 Creating An Android App From The Command Line

In the ~/tweetme/tweetme/ directory, run:
droidgap create
(If you get the error message droidgap: command not found, please run
export PATH="$HOME/android-sdk-linux_86/tools:$HOME/android-sdk-linux_86/platform-tools:$HOME/phonegap-phonegap-android/bin:$PATH"
and try again. )
This will create the directory ~/tweetme/tweetme_android which contains everything we need to build our final app from it. The~/tweetme/tweetme_android/assets/www/ directory contains our HTML, CSS, JavaScript sources plus the phonegap.js file.
cd ../tweetme_android/
ls -l
falko@falko-desktop:~/tweetme/tweetme_android$ ls -l
total 44
-rw-r--r-- 1 falko falko 2000 2011-01-24 18:43 AndroidManifest.xml
drwxr-xr-x 3 falko falko 4096 2011-01-24 18:43 assets
drwxr-xr-x 2 falko falko 4096 2011-01-24 18:43 bin
-rw-r--r-- 1 falko falko  696 2011-01-24 18:43 build.properties
-rw-r--r-- 1 falko falko 3201 2011-01-24 18:43 build.xml
-rw-r--r-- 1 falko falko  362 2011-01-24 18:43 default.properties
drwxr-xr-x 2 falko falko 4096 2011-01-24 18:43 libs
-rw-r--r-- 1 falko falko  424 2011-01-24 18:43 local.properties
-rw-r--r-- 1 falko falko 1034 2011-01-24 18:43 proguard.cfg
drwxr-xr-x 7 falko falko 4096 2011-01-24 18:43 res
drwxr-xr-x 3 falko falko 4096 2011-01-24 18:43 src
falko@falko-desktop:~/tweetme/tweetme_android$
ls -l assets/www/
falko@falko-desktop:~/tweetme/tweetme_android$ ls -l assets/www/
total 200
-rw-r--r-- 1 falko falko   4311 2011-01-24 15:59 index.html
drwxr-xr-x 2 falko falko   4096 2011-01-24 15:57 jqtouch
-rw-r--r-- 1 falko falko  78601 2011-01-24 15:57 jquery.js
-rw-r--r-- 1 falko falko 105761 2011-01-24 15:57 phonegap.js
drwxr-xr-x 4 falko falko   4096 2011-01-24 15:57 themes
falko@falko-desktop:~/tweetme/tweetme_android$
The libs/ directory contains the file phonegap.jar:
ls -l libs/
falko@falko-desktop:~/tweetme/tweetme_android$ ls -l libs/
total 120
-rw-r--r-- 1 falko falko 122227 2011-01-24 15:57 phonegap.jar
falko@falko-desktop:~/tweetme/tweetme_android$
Now we must edit the index.html file in the assets/www/ folder and add the phonegap.js file to the <head></head> section (before all other JavaScript files/JavaScript code).
gedit assets/www/index.html
Lets assume the file starts as follows:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Tweetme</title>

    <!-- include JQuery through Google API => Always have the latest version -->
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript"> google.load("jquery", "1.3.2"); </script>

    <!-- import JQTouch -->
    <script src="jqtouch/jqtouch.min.js" type="application/x-javascript" charset="utf-8"></script>

    <!-- Import JQTouch default style (iPhone look).
       Replace the string "themes/apple" with "themes/jq" for a non-iPhone theme -->
    <link type="text/css" rel="stylesheet" media="screen" href="jqtouch/jqtouch.css">
    <link type="text/css" rel="stylesheet" media="screen" href="themes/apple/theme.css">
[...]
Add the line <script type="text/javascript" charset="utf-8" src="phonegap.js"></script> before all other JavaScript so that it looks as follows:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Tweetme</title>
    <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
    <!-- include JQuery through Google API => Always have the latest version -->
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript"> google.load("jquery", "1.3.2"); </script>

    <!-- import JQTouch -->
    <script src="jqtouch/jqtouch.min.js" type="application/x-javascript" charset="utf-8"></script>

    <!-- Import JQTouch default style (iPhone look).
       Replace the string "themes/apple" with "themes/jq" for a non-iPhone theme -->
    <link type="text/css" rel="stylesheet" media="screen" href="jqtouch/jqtouch.css">
    <link type="text/css" rel="stylesheet" media="screen" href="themes/apple/theme.css">
[...]
Now, still in the ~/tweetme/tweetme_android directory, we can finally build our app:
ant debug
It should say BUILD SUCCESSFUL at the end:
...
-package-debug-sign:
[apkbuilder] Creating tweetme-debug-unaligned.apk and signing it with a debug key...

debug:
     [echo] Running zip align on final apk...
     [echo] Debug Package: /home/falko/tweetme/tweetme_android/bin/tweetme-debug.apk

BUILD SUCCESSFUL
Total time: 28 seconds
falko@falko-desktop:~/tweetme/tweetme_android$
You can now find your app in the bin/ directory (called tweetme-debug.apkapk is the extension for Android apps):
cd bin/
ls -l
falko@falko-desktop:~/tweetme/tweetme_android/bin$ ls -l
total 764
drwxr-xr-x 3 falko falko   4096 2011-01-24 16:00 classes
-rw-r--r-- 1 falko falko 140552 2011-01-24 16:01 classes.dex
-rw-r--r-- 1 falko falko 166392 2011-01-24 16:01 tweetme.ap_
-rw-r--r-- 1 falko falko 232458 2011-01-24 16:01 tweetme-debug.apk
-rw-r--r-- 1 falko falko 232405 2011-01-24 16:01 tweetme-debug-unaligned.apk
falko@falko-desktop:~/tweetme/tweetme_android/bin$
To install it to the first running emulator, we can run
adb -e install -r tweetme-debug.apk
falko@falko-desktop:~/tweetme/tweetme_android/bin$ adb -e install -r tweetme-debug.apk
660 KB/s (232458 bytes in 0.343s)
        pkg: /data/local/tmp/tweetme-debug.apk
Success
falko@falko-desktop:~/tweetme/tweetme_android/bin$
(Instead of running ant debug first and then adb -e install -r tweetme-debug.apk from the bin/ directory to install the app, we could have run
ant debug install
which would have built and afterwards installed the app in one go.)
Now go to your emulator, and you should find the new app listed among the other apps:
Click to enlarge
This is what the app looks like:
Click to enlarge
Click to enlarge

7.2 Creating An Android App From Eclipse

In the ~/tweetme/tweetme/ directory, run:
droidgap create
(If you get the error message droidgap: command not found, please run
export PATH="$HOME/android-sdk-linux_86/tools:$HOME/android-sdk-linux_86/platform-tools:$HOME/phonegap-phonegap-android/bin:$PATH"
and try again. )
This will create the directory ~/tweetme/tweetme_android which contains everything we need to build our final app from it. The~/tweetme/tweetme_android/assets/www/ directory contains our HTML, CSS, JavaScript sources plus the phonegap.js file.
cd ../tweetme_android/
ls -l
falko@falko-desktop:~/tweetme/tweetme_android$ ls -l
total 44
-rw-r--r-- 1 falko falko 2000 2011-01-24 18:43 AndroidManifest.xml
drwxr-xr-x 3 falko falko 4096 2011-01-24 18:43 assets
drwxr-xr-x 2 falko falko 4096 2011-01-24 18:43 bin
-rw-r--r-- 1 falko falko  696 2011-01-24 18:43 build.properties
-rw-r--r-- 1 falko falko 3201 2011-01-24 18:43 build.xml
-rw-r--r-- 1 falko falko  362 2011-01-24 18:43 default.properties
drwxr-xr-x 2 falko falko 4096 2011-01-24 18:43 libs
-rw-r--r-- 1 falko falko  424 2011-01-24 18:43 local.properties
-rw-r--r-- 1 falko falko 1034 2011-01-24 18:43 proguard.cfg
drwxr-xr-x 7 falko falko 4096 2011-01-24 18:43 res
drwxr-xr-x 3 falko falko 4096 2011-01-24 18:43 src
falko@falko-desktop:~/tweetme/tweetme_android$
ls -l assets/www/
falko@falko-desktop:~/tweetme/tweetme_android$ ls -l assets/www/
total 200
-rw-r--r-- 1 falko falko   4311 2011-01-24 15:59 index.html
drwxr-xr-x 2 falko falko   4096 2011-01-24 15:57 jqtouch
-rw-r--r-- 1 falko falko  78601 2011-01-24 15:57 jquery.js
-rw-r--r-- 1 falko falko 105761 2011-01-24 15:57 phonegap.js
drwxr-xr-x 4 falko falko   4096 2011-01-24 15:57 themes
falko@falko-desktop:~/tweetme/tweetme_android$
The libs/ directory contains the file phonegap.jar:
ls -l libs/
falko@falko-desktop:~/tweetme/tweetme_android$ ls -l libs/
total 120
-rw-r--r-- 1 falko falko 122227 2011-01-24 15:57 phonegap.jar
falko@falko-desktop:~/tweetme/tweetme_android$
Now open Eclipse and go to New > Project...:
Click to enlarge
In the New Project window, select Android > Android Project and click on Next >:
Click to enlarge
In the New Android Project window, select Create project from existing source (we have previously created the source by running droidgap create) and click on the Browse... button - don't fill out any other fields in that window:
Click to enlarge
Select the ~/tweetme/tweetme_android directory and click on OK:
Click to enlarge
Back in the New Android Project window, all other fields should now be filled. Click on Finish:
Click to enlarge
You should now see the source tree of your app in the left panel in Android. Right-click the assets/www/index.html file and select Open With > Text Editor:
Click to enlarge
Add the phonegap.js file to the <head></head> section (before all other JavaScript files/JavaScript code).
Lets assume the file starts as follows:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Tweetme</title>

    <!-- include JQuery through Google API => Always have the latest version -->
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript"> google.load("jquery", "1.3.2"); </script>

    <!-- import JQTouch -->
    <script src="jqtouch/jqtouch.min.js" type="application/x-javascript" charset="utf-8"></script>

    <!-- Import JQTouch default style (iPhone look).
       Replace the string "themes/apple" with "themes/jq" for a non-iPhone theme -->
    <link type="text/css" rel="stylesheet" media="screen" href="jqtouch/jqtouch.css">
    <link type="text/css" rel="stylesheet" media="screen" href="themes/apple/theme.css">
[...]
Add the line <script type="text/javascript" charset="utf-8" src="phonegap.js"></script> before all other JavaScript so that it looks as follows:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Tweetme</title>
    <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
    <!-- include JQuery through Google API => Always have the latest version -->
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript"> google.load("jquery", "1.3.2"); </script>

    <!-- import JQTouch -->
    <script src="jqtouch/jqtouch.min.js" type="application/x-javascript" charset="utf-8"></script>

    <!-- Import JQTouch default style (iPhone look).
       Replace the string "themes/apple" with "themes/jq" for a non-iPhone theme -->
    <link type="text/css" rel="stylesheet" media="screen" href="jqtouch/jqtouch.css">
    <link type="text/css" rel="stylesheet" media="screen" href="themes/apple/theme.css">
[...]
Now save the file.
Click to enlarge
Next right-click the libs/phonegap.jar file and select Build Path > Add to Build Path:
Click to enlarge
Now right-click the root of our source tree, tweetme, and select Run As > Android Application:
Click to enlarge
This will open our emulator and automatically start our app:
Click to enlarge

8 Using A Real Android Device

If you have a real Android smartphone and want to test your app on this device instead of the emulator, you have to first enable USB debugging on the device (Menu > Settings > Applications > Development > USB-Debugging) and then plug it into your Ubuntu desktop using the device's USB cable.
Now check if Ubuntu has correctly identified the device:
adb devices
If you see a lot of question marks like this...
falko@falko-desktop:~$ adb devices
List of devices attached
????????????        no permissions

falko@falko-desktop:~$
... then Ubuntu did not identify your device. In this case create the file /etc/udev/rules.d/51-android.rules...
sudo gedit /etc/udev/rules.d/51-android.rules
... with the following contents:
SUBSYSTEM=="usb", SYSFS{idVendor}=="0bb4", MODE="0666"
Replace 0bb4 (this is for HTC phones) with the correct vendor ID which you can find here: http://developer.android.com/guide/developing/device.html#VendorIds
Then run:
sudo chmod a+r /etc/udev/rules.d/51-android.rules
Plug out your phone and plug it back in, and Ubuntu should now recognize it:
adb devices
falko@falko-desktop:~$ adb devices
List of devices attached
SH0ARPL12791        device

falko@falko-desktop:~$
If you have your app as an apk file, you can now install it onto your phone as follows:
adb -d install -r tweetme-debug.apk
(Please note that I use -d (for device) instad of -e (for emulator) here.)

9 Links

Bài đăng phổ biến