Android Programming/zh CN

From Free Pascal wiki
Revision as of 15:36, 12 July 2015 by FTurtle (talk | contribs)
Jump to navigationJump to search

English (en) 日本語 (ja) 한국어 (ko) русский (ru) 中文(中国大陆)‎ (zh_CN)

其它请参考 Custom Drawn Interface/Android

了解普通的Android 程序开发可以非常有用的帮助Lazarus Android界面的开发。

如何做...

构建 NDK OpenGL 示例

操作步骤如下:

第1步 - 下载和安装Android NDK, Android SDK 和 Ant.更多的信息在这儿: Android_Interface/Using_the_Android_SDK,_Emulator_and_Phones

第2步 - 安装已经编译好的FPC跨平台编译器. 说明信息在这儿: Android_Interface#Using_the_pre-compiled_compiler

第3步 - 下载最新的 lazarus-ccr sourceforge 代码:

svn co https://lazarus-ccr.svn.sourceforge.net/svnroot/lazarus-ccr lazarus-ccr

或者如果你觉得这个文件夹太大那么你可以只下载此文件夹 lazarus-ccr/bindings/android-ndk

第4步 - 构建Pascal库文件(.SO)

打开工程 lazarus-ccr/bindings/android-ndk/examples/opengltest/opengltest.lpi

然后打开菜单 Project->Project Options->Paths 和右边的 "Libraries -Fl" 你可以看到如下所示:

/home/felipe/Programas/android-ndk-r5/platforms/android-9/arch-arm/usr/lib

更改Android NDK路径为你的电脑中Andorid NDK的路径, 并且更改最低需要的Android API版本库文件夹路径为你的电脑的路径.

现在使用Lazarus构建工程.

第5步 - 配置构建环境变量

打开文件 opengltest/local.properties 查看这一行:

sdk.dir=/home/felipe/Programas/android-sdk-linux

更改为你的Android SDK的路径, 注意 "/" 和 "\"的区别

第6步 - 构建 APK 包

打开一个终端(命令行)并且输入如下命令:

cd lazarus-ccr/bindings/ndk/examples/opengltest/android ant debug

成功构建的APK包位于这个文件夹中: opengltest/android/bin/

第7步 - 安装 APK 包

如果在这一步你得到关于权限的错误信息,可以阅读这儿: Android_Interface/Using_the_Android_SDK,_Emulator_and_Phones#Recognition_of_devices_under_Linux

使用命令行安装我们生成的APK包到手机:

cd opengltest/android
~/Programas/android-sdk-linux/platform-tools/adb install bin/OpenGLNDKTest-debug.apk 
2395 KB/s (107299 bytes in 0.043s)
       pkg: /data/local/tmp/OpenGLNDKTest-debug.apk
Success

如果你得到一个包已经安装的错误信息那么你可以使用以下命令来卸载它:

~/Programas/android-sdk-linux/platform-tools/adb uninstall com.pascal.opengltest

然后你可以在命令行中运行adb logcat,可以看到这个log记录, 它可以告诉你是如何在手机中添加一个新图标应用的:

~/Programas/android-sdk-linux/platform-tools/adb logcat

读取屏幕尺寸

First fill a TDisplayMetrics with the correct info:

uses androidutil;

var
  MyDisplayMetrics: TDisplayMetrics;
  Str: string;
  //
  lHeight, lWidth: Integer;
  xdpi, ydpi, lScreenSize: Single;
begin
  // ..
  // Objects

  MyDisplayMetrics := TDisplayMetrics.Create;
  Activity.getWindowManager().getDefaultDisplay().getMetrics(MyDisplayMetrics);

And then you can read it from the TDisplayMetrics:

  lHeight := MyDisplayMetrics.heightPixels();
  lWidth := MyDisplayMetrics.widthPixels();
  xdpi := MyDisplayMetrics.xdpi();
  ydpi := MyDisplayMetrics.ydpi();
  lScreenSize := sqrt(sqr(lHeight / ydpi) + sqr(lWidth / xdpi));
  ldensity := MyDisplayMetrics.density();
  ldensityDpi := MyDisplayMetrics.densityDpi();
  scaledDensity := MyDisplayMetrics.scaledDensity();

Note that lots of devices lie about the xdpi and ydpi, so don't trust the lScreenSize calculated above. Smartphones might report even 10 inches of screen size, while the correct is around 4.

关于Android 设备的详细描述

Since there are so many Android devices, it can be useful to keep track of some information about them. See also in the wikipedia Comparison of Android Devices [1] and details about the processors in [2].

智能手机

Manufacturer Model Android API Name (Build.Model) Processor Android version Multi-touch Comments
HTC Wildfire HTC Wildfire armv6 2.1=>2.2 Yes -
Sony Erricson Xperia X10 X10i armv7 1.6=>2.1 No -

平板

Manufacturer Model Android API Name (Build.Model) Processor Android version Multi-touch Comments
Toshiba Folio 100 Tablet TOSHIBA_FOLIO_AND_A armv7 2.2 Yes -

处理器兼容设备列表(ARMv6 vs ARMv7)

ARMv7 设备列表:

  • Advent Vega (P10AN01)
  • Dell Streak, Streak 7
  • HTC Desire
  • HTC Desire Z (T-Mobile G2)
  • HTC Desire HD
  • HTC Droid Incredible
  • HTC EVO 4G, EVO Shift 4G
  • HTC Glacier (T-Mobile myTouch 4G)
  • HTC Inspire 4G
  • HTC Nexus One
  • HTC Thunderbolt 4G
  • Huawei Ideos S7
  • LG Optimus Z
  • Motorola Atrix 4G
  • Motorola Bravo
  • Motorola Cliq 2 - untested
  • Motorola Defy
  • Motorola Droid 2, Droid 2 Global
  • Motorola Droid Pro (Motorola PRO)
  • Motorola Droid X
  • Motorola Xoom
  • POV Mobii Tegra Tablet
  • Samsung Continuum (i400)
  • Samsung Galaxy S (i9000, Captivate, Fascinate, Vibrant, Epic 4G)
  • Samsung Galaxy Tab
  • Sharp IS03
  • Sony Ericsson Xperia X10
  • Toshiba AS100
  • Viewsonic gTablet
  • Acer Liquid E
  • Acer Liquid (Liquid A1)
  • Archos 101 Internet Tablet
  • Motorola Charm
  • Motorola Droid (Milestone)
  • Samsung Galaxy S 4G
  • Samsung Nexus S

ARMv6 处理器列表:

  • Android SDK emulator
  • Asus Garmin nuvifone A50 (T-Mobile Garminfone)
  • Augen GENTouch 78 Tablet
  • Coby Kyros Internet Tablet (MID7015)
  • Geeksphone One, Geeksphone Zero
  • HTC Aria
  • HTC ChaCha
  • HTC Dream (T-Mobile G1, Android Dev Phone 1)
  • HTC Droid Eris
  • HTC Espresso (T-Mobile myTouch 3G Slide)
  • HTC Hero (T-Mobile G2 Touch)
  • HTC Legend
  • HTC Magic (T-Mobile myTouch 3G, T-Mobile G1 Touch)
  • HTC Salsa
  • HTC Tattoo
  • HTC Wildfire
  • Huawei Ascend
  • Huawei Ideos U8150-B (T-Mobile Comet)
  • Huawei U8110 (T-Mobile Pulse Mini)
  • Huawei U8230
  • LG Ally (Apex) (LG VS740)
  • LG GW620 (Eve, InTouch Max, LinkMe)
  • LG Optimus, Optimus M, Optimus T, Optimus S, Optimus V
  • LG Vortex
  • LG P500
  • MAG iMiTO iM7
  • vMAG iMiTO iM7S
  • Motorola Backflip
  • Motorola Citrus
  • Motorola Cliq (MB200)
  • Motorola Dext
  • Motorola Devour
  • Motorola i1
  • Motorola Spice XT300
  • Motorola Quench XT5 XT502
  • Pandigital Novel
  • Samsung GT-S5570 Galaxy Mini
  • Samsung i5500 Galaxy 5 (Corby)
  • Samsung i5700 Galaxy Portal (Spica)
  • Samsung i5800 Galaxy 3
  • Samsung i7500 Galaxy
  • Samsung Intercept
  • Samsung M900 Moment
  • Samsung S5830 Galaxy Ace
  • Samsung Transform
  • Sanyo ZIO M6000
  • Sony Ericsson Xperia X8
  • Sony Ericsson Xperia X10 Mini
  • Sony Ericsson Xperia X10 Mini Pro
  • Superpad 10.2" Tablet PC
  • Viewsonic ViewPad 7 Tablet
  • Velocity Micro T103 Cruz tablet
  • Vodafone 845
  • ZTE Blade

在手机中播放声音和视频

每一个手机都安装有不同的解码器, 这样意味着不同的手机中本地媒体播放器可以播放不同的格式。以下表格列出了哪些格式可以在哪个手机中的媒体播放器中播放:

Telephone Android webm - VP8 m4v - H.264 ogv - Theora mp4 - H.264 mov - H.264 avi - RLE mpg - MPEG-1 wmv - WM9 3gp - MPEG-4
Emulator 1.6 x x x x x x x x OK
Emulator 2.1 x x x x x x x x OK
Nexus One 2.2 x OK x OK x x x x OK
HTC Desire 1.6 x OK x OK x x x OK OK
Toshiba Folio 100 2.2 x OK x OK x x x OK OK
Xperia X10 2.1 x OK x x x x x OK OK
Xperia X8 2.2 x OK x x x x x x OK
HTC Wildfire 2.1 x OK x x x x x x OK
HTC Desire HD 2.2 x OK x OK x x x x OK
Galaxy Tab 2.2 x OK x x x x x x OK
myPhone A210 1.6 x OK x x x x x x OK
Motorola Milestone 2.1 x OK x x x x x OK ?
Milestone 2 2.2 x OK x x x x x sound-only OK

新建一个Java Android 应用程序

这些信息可以帮助用于实现 LCL-CustomDrawn-Android

通用指令在这儿: http://developer.android.com/guide/developing/other-ide.html

显示 / 隐藏 虚拟键盘

请看这儿: http://android-codes-examples.blogspot.com/2011/11/show-or-hide-soft-keyboard-on-opening.html

显示虚拟键盘:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);

隐藏虚拟键盘:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(singleedittext.getWindowToken(),0);

参考