Delphi XE5如何透過URL呼叫Android或iOS上的其他程式?

之前曾經分享過,手機版的Line可以透過 line://msg/<CONTENT TYPE>/<CONTENT KEY> 這樣的語法呼叫Line 的程式分享。

當然Delphi XE5也可以透過程式呼叫程式才對,在 的「Sending a URL to Another App on Android and iOS with Delphi XE5」文章有教您如何呼叫

  • http, tel, sms, fb, mailto, twitter, geo…..
  • 作者也分類成三種:
  • 1. iOS跟Android都可以使用的URLs
  • http://superlevin.ifengyuan.tw/
  • tel://0921789779
  • sms://hello_world
  • http://twitter.com/superlevin (這個語法會直接打開Android上的Twitter Client程式)
  • mailto://superlevin@gmail.com
  • twitter://user?screen_name=superlevin
  • fb://profile/705666540
    (可以透過 http://graph.facebook.com/(帳號)superlevin取得UID)

iOS特殊語法

  • http://maps.apple.com?q=5617 Scotts Valley Drive, Scotts Valley, CA 95066 (需要URL encode)

Android特殊語法

  • content://contacts/people/
  • content://contacts/people/1
  • geo://0,0?q=5617 Scotts Valley Drive, Scotts Valley, CA 95066
  • geo://46.191200, -122.194400 (I think this one doesn’t like the URLEncode)

作者在文中有提到使用TidURL.URLEncode 。

[pascal]
unit OpenViewUrl;

interface

// URLEncode is performed on the URL
// so you need to format it protocol://path
function OpenURL(const URL: string; const DisplayError: Boolean = False): Boolean;

implementation

uses
IdURI, SysUtils, Classes, FMX.Dialogs,
{$IFDEF ANDROID}
FMX.Helpers.Android, Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.Net, Androidapi.JNI.JavaTypes;
{$ELSE}
{$IFDEF IOS}
iOSapi.Foundation, FMX.Helpers.iOS;
{$ENDIF IOS}
{$ENDIF ANDROID}

function OpenURL(const URL: string; const DisplayError: Boolean = False): Boolean;
{$IFDEF ANDROID}
var
Intent: JIntent;
begin
// There may be an issue with the geo: prefix and URLEncode.
// will need to research
Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW,
TJnet_Uri.JavaClass.parse(StringToJString(TIdURI.URLEncode(URL))));
try
SharedActivity.startActivity(Intent);
exit(true);
except
on e: Exception do
begin
if DisplayError then ShowMessage(‘Error: ‘ + e.Message);
exit(false);
end;
end;
end;
{$ELSE}
{$IFDEF IOS}
var
NSU: NSUrl;
begin
// iOS doesn’t like spaces, so URL encode is important.
NSU := StrToNSUrl(TIdURI.URLEncode(URL));
if SharedApplication.canOpenURL(NSU) then
exit(SharedApplication.openUrl(NSU))
else
begin
if DisplayError then
ShowMessage(‘Error: Opening "’ + URL + ‘" not supported.’);
exit(false);
end;
end;
{$ELSE}
begin
raise Exception.Create(‘Not supported!’);
end;
{$ENDIF IOS}
{$ENDIF ANDROID}

end.
[/pascal]

Facebook大當機?

晚上突然發現Facebook不能留言、按讚等更新狀態….結果才發現是全球性的問題。希望能快快好起來!

螢幕快照 2013-10-21 下午8.41.26 螢幕快照 2013-10-21 下午8.45.07

 

Facebook status update problems aren’t uncommon, but this one seems global — we’ve tested it from several IPs, receiving the same message.

In addition to status update issues, we were also unable to Like or post photos on Facebook.

According to web service status tracker Downrightnow, Facebook is currently likely experiencing a service disruption.

We’ve contacted Facebook about the issue but have not yet received a response.

Stay tuned for updates.

Image: Facebook

 

相關連結: Mashable

Delphi XE5 不同平台使用 SSL/HTTPS

以前在 Delphi 使用 Tidhttp元件時,需要額外載入 OpenSSL的 dll。Delphi XE5開始支援多裝置、多平台的時候,該如何解決呢?

昨天在 Coderage 8時,Olaf Monien的分享,記錄下來避免忘記!不過文中提到Android目前似乎還沒有XD

 

 

連結:

Delphi XE5 : SSL / HTTPS on different platforms with TIdHTTP and TRESTClient


http://www.monien.net/delphi-xe5-ssl-https-on-different-platforms-with-tidhttp-and-trestclient/

Delphi XE LiveBindings連結MySQL中文亂碼問題

 

 

 

一直都嘗試用原來的寫法,沒試過LiveBindings的功能,昨天看到 CodeRage8的介紹後,今天嘗試一下,結果剛好遇到MySQL中文亂碼問題,一起分享。

Data Explorer

DataExplorer01

1打開Data Explorer,在MySQL按右鍵 >> Add New ConnectionDataExplorer02

2新增名字DataExplorer03

3輸入連線資訊

HostName >> 主機名稱

Database >> 資料庫名稱

User Name >> 帳號
Password >> 密碼

接著可以打開 Advanced看看裡面有什麼

DataExplorer04 DataExplorer05

4按OK完成

 

LiveBindings

liveBindings01

1從上面的功能表點選 View -> LiveBindings Designer打開 LiveBindings DesignerliveBindings02

2會看到下面多了一塊 LiveBindings Designer的畫面,接著按左下角的LiveBindings Wizard

liveBindings03

3可以選擇Binding的方式,我們選第二個 Link a grid with a data source

liveBindings04

4選擇TGrid

liveBindings05

5選DBX(DBExpress)

liveBindings06

6選擇 Driver: MySQL;Connection Name:POS(剛才Data Explorer建立的);Command Type: ctTable(連接Table);Command Text:下拉要連結的Table

liveBindings07

7勾選 Add Data Source navigator,新增一個navigatorliveBindings08

8完成後就會發現 LiveBinding都做好了XD 稍徵排版一下liveBindings09

9發現裡面的中文變成????怎麼辦?

liveBindings10

10點選 TSQLConnection,在Driver裡面有一個 ServerCharSet設成big5,接著把上面的Connected勾掉再勾回去(重新連線)

liveBindings11

11再回到TBindSourceDBX,一樣在Active的地方勾掉再勾回去重新連線

liveBindings1212亂碼的地方回復成中文了!

 

Day4 – Delphi XE5 簡易BMI計算器

今天教大家設計BMI計算機,螢幕快照 2013-10-12 下午12.30.53

1首先開啟一個空白專案

螢幕快照 2013-10-12 下午12.31.17

2新增一個ListBox

螢幕快照 2013-10-12 下午12.31.31

3將aligh 設為 alClient(填滿)

螢幕快照 2013-10-12 下午12.31.53

4將GroupingKind 設成 gsGrouped

螢幕快照 2013-10-12 下午12.32.17

5將StyleLookup 設定 transparentlistboxstyle

itemeditor

6在畫面上按右鍵開啟 Items Editor

itemsdesigner1

7下拉TListBoxGroupHeader後按Add Item

itemsdesigner2

8接著下拉 TListBoxItem 後按 Add Item

螢幕快照 2013-10-12 下午12.33.24

9新增如上圖

螢幕快照 2013-10-12 下午12.34.04

10點上面的LiboxGroupHeader1修改Text 為 BMI計算機,TextAligh設為 taCenter(置中)螢幕快照 2013-10-12 下午12.34.48

11陸續修改其他如上圖

螢幕快照 2013-10-12 下午12.35.16

12新增TEdit元件

螢幕快照 2013-10-12 下午12.35.34

13修改屬性Aligh 為 alRight

螢幕快照 2013-10-12 下午12.36.23

14將KeyboardType改成 vktNumberPad (預設開啟數字鍵盤)

螢幕快照 2013-10-12 下午12.36.50

15新增一個Button

螢幕快照 2013-10-12 下午12.37.06

16將Aligh 設為 alClient

螢幕快照 2013-10-12 下午12.37.24

17 Text設為計算

螢幕快照 2013-10-12 下午12.37.44

18新增Label

螢幕快照 2013-10-12 下午12.38.17

19設定完畫面如圖

螢幕快照 2013-10-12 下午12.38.35

20將edit1.text,edit2.text的TextAligh屬性設為 taTrailing (置右)

螢幕快照 2013-10-12 下午12.39.34

21都設定完之後,可以下拉成不同機器

螢幕快照 2013-10-12 下午12.39.03 螢幕快照 2013-10-12 下午12.39.16

22可以看得到在ios、android會自動轉換成各自的風格

sourcecode

23接著在畫面的計算按鈕點二下,輸入上圖的程式

[pascal]
procedure TForm1.Button1Click(Sender: TObject);
var
BMI:double;
begin
BMI := StrToFloat(FormatFloat(‘#.##’,(StrToFloat(Edit2.text) /
( (StrToFloat(Edit1.Text)/ 100) * (StrToFloat(Edit1.Text)/100 )))));
Label1.Text := FloatToStr(BMI);
end;
[/pascal]

螢幕快照 2013-10-12 下午12.41.36

24 可以雙點不同的平台,看看跑出來的效果

螢幕快照 2013-10-12 下午12.42.16

25 Windows

螢幕快照 2013-10-12 下午12.47.44

螢幕快照 2013-10-12 下午12.48.00

26 iPhone

1386014_10151624432496541_1740355845_n 1382787_10151624432376541_1638450671_n

27  Android

Github 程式下載 https://github.com/superlevin/delphixe5bmicalc

 

Delphi XE5 在 Windows跟Mobile(Android/iOS)字串處理方式不同

在Delphi XE5中開發移動裝置(Android/iOS)有不同的編譯器

iOS模擬器:DCCIOS32.EXE
iOS實 機:DCCIOSARM.EXE
A
ndroid  :DCCAARM.EXE

所以在開發時,有些資料型態是必需改寫的:

WideString, AnsiString, ShortString, AnsiChar, PAnsiChar, PWideChar, Openstring

而下面的表格有教您如何做對應處理。

Migrating Delphi Code to Mobile from Desktop

接下來的是0-based跟1-based,以前的程式都是從1開始,在mobile complier上則是從0開始(文件上說以後可能會變成標準)。

Migrating Delphi Code to Mobile from Desktop - RAD Studio

官方建議是使用StringHelper、或StringBuilder來處理(例如Pos改成Indexof….)

Migrating Delphi Code to Mobile from Desktop1 - RAD Studio

參考資料

Migrating Delphi Code to Mobile from Desktop

 

Day3 – Delphi XE5簡易手電筒

在經過前二天的安裝以及設定之後,今天來教大家寫一個最簡單的app-手電筒。十分鐘就可以完成了唷 🙂

螢幕快照 2013-10-10 下午8.43.051新增空白專案

螢幕快照 2013-10-10 下午8.56.04

2接著我們要在畫面上放上二個元件,在右下角的元件盤中搜尋打上tswitch,這是一個開關的元件。直接在TSwitch快點二下

螢幕快照 2013-10-10 下午8.56.28

3第二個元件是TCameraComponent,從名稱就知道跟相機有關的,沒錯,因為手電筒就是打開相機的閃光燈。一樣也是搜尋到之後快點二下即可。

螢幕快照 2013-10-10 下午9.16.09

4二個元件放上去的畫面

螢幕快照 2013-10-10 下午8.56.45

5接下來點選畫面上的CameraComponent1元件,然後左下角的Active打勾成為 True

螢幕快照 2013-10-10 下午8.57.05

6接下來點選畫面上的Switch元件,然後將左下角的屬性視窗點Events頁籤,往下找到OnSwitch快點右邊空白處二下

螢幕快照 2013-10-10 下午8.59.57

7在裡面輸入以下的程式

[pascal]
if Switch1.IsChecked then begin
if CameraComponent1.HasFlash then
CameraComponent1.TorchMode := TTorchMode.tmModeOn;
end else begin
if CameraComponent1.HasFlash then
CameraComponent1.TorchMode := TTorchMode.tmModeOff;
end;
[/pascal]

稍微講解一下,如果Switch是打開的(Switch1.IsChecked),檢查相機是不是有閃光燈(CameraComponent1.HasFlash),確定有的話,就將相機的TorchMode(燈光)打開(tmModeOn),相反的就是關掉(tmModeOff)。

打完收功!按下F9之後,就是您的第一個APP程式叫「手電筒」XD

範例程式碼下載: https://github.com/superlevin/delphixe5flashlight

實機操作畫面: