雖然我發現,stackoverflow的分享功能很方便
但還是在我blog留點資料好了 ~"~
對新手一定要這麼殘忍嗎??
一起手馬上遇到這難搞的R.java問題.
如果一直卡在R.xx.xx cannot be resolved or is not a field.
最有可能就是class import了 android.R 這個壞東西,
請改成packagename.R 就搞定了~呼.
花了我好幾天時間, 搞得我都快崩潰了的爛問題!
希望能幫到快要絕望的人!
資料來源: stackoverflow
2013年9月9日 星期一
2013年9月2日 星期一
使用WebView來顯示google街景地圖
緣起
2012年底新版的google map開始就支援街景地圖,
如今google官方也都有開api出來取用街景.
需求
實作
如果app不想讓user跳出程式來觀看google街景地圖,
也不想k一堆官方文件, 用WebView來嵌入是最快選擇了.
需求
有gps座標資料, 然後想要顯示該地區的街景
實作
比較老土的作法是把下面的string存成一份html檔案在local,
然後讀出html後轉成string再把經緯度給填上.
後來發現也不用這麼搞, 直接寫一份就對了!
完成圖如下:
NSString *htmlString = [NSString stringWithFormat:@"<html>\
<head>\
<meta id=\"viewport\" name=\"viewport\" content=\"width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;\">\
<script src='http://maps.google.com/maps/api/js?sensor=false' type='text/javascript'></script>\
</head>\
<body onload=\"new google.maps.StreetViewPanorama(document.getElementById('p'),{position:new google.maps.LatLng(%f, %f)});\" style='padding:0px;margin:0px;'>\
<div id='p' style='height:460;width:320;'></div>\
</body>\
</html>",lat, lon];
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
webView.scalesPageToFit = YES;
webView.scrollView.scrollEnabled = NO;
webView.scrollView.bounces = NO;
webView.delegate = self;
[webView loadHTMLString:htmlString baseURL:nil];
完成圖如下:
2013年8月30日 星期五
在iOS6 使用facebook分享文章的功能
緣起
記得早先在寫iOS app要在上面分享個東西到facebook上面,
除了研讀facebook的api外, code也比較複雜一點.
現在iOS6的social framework已經有支援了,不拿來用用太可惜了.
當然要客製化, 讀取朋友列表等等還是得回頭k一下facebook的官方文件啦.
需求
這次只是很簡單的想要在 "個人的動態消息" 上分享訊息
訊息包含了"文字描述", "URL link", "圖片"
分析一下發現官方提供的東西已經很夠用了.
於是讓我們來看看要怎麼實作吧~
實作
SDK最低需求:iOS6
xcode版本: 4.5或更新(筆者使用4.6.3)
1. project > targets > Build Phases
添加Social.framework
2. 在viewController.m 底下
#import
3. 在自己建立好的button event裡面
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[controller setInitialText:@"分享文章的內容"];
[controller addURL:[NSURL URLWithString:@"分享文章的url link"]];
[controller addImage:[UIImage imageNamed:@"圖片的來源"]];
[self presentViewController:controller animated:YES completion:Nil];
}
其中如果使用了非同步的圖片下載, 那或許可以參考筆者的作法 (ex: SDWebImage)
UIImageView *postimg = [UIImageView new];
[postimg setImageWithURL:[NSURL URLWithString:@"url link"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]
completed:^(UIImage *image, NSError *err, SDImageCacheType typ) {
/* 等待圖片載完後才addImage */
[controller addImage:postimg.image];
}];
這樣就完成了耶! 分享給需要的人.
下面的link還有twitter的範例, 相當清晰易懂!
參考資料:http://www.appcoda.com/ios-programming-101-integrate-twitter-and-facebook-sharing-in-ios-6/
記得早先在寫iOS app要在上面分享個東西到facebook上面,
除了研讀facebook的api外, code也比較複雜一點.
現在iOS6的social framework已經有支援了,不拿來用用太可惜了.
當然要客製化, 讀取朋友列表等等還是得回頭k一下facebook的官方文件啦.
需求
這次只是很簡單的想要在 "個人的動態消息" 上分享訊息
訊息包含了"文字描述", "URL link", "圖片"
分析一下發現官方提供的東西已經很夠用了.
於是讓我們來看看要怎麼實作吧~
實作
SDK最低需求:iOS6
xcode版本: 4.5或更新(筆者使用4.6.3)
1. project > targets > Build Phases
添加Social.framework
2. 在viewController.m 底下
#import
3. 在自己建立好的button event裡面
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[controller setInitialText:@"分享文章的內容"];
[controller addURL:[NSURL URLWithString:@"分享文章的url link"]];
[controller addImage:[UIImage imageNamed:@"圖片的來源"]];
[self presentViewController:controller animated:YES completion:Nil];
}
其中如果使用了非同步的圖片下載, 那或許可以參考筆者的作法 (ex: SDWebImage)
UIImageView *postimg = [UIImageView new];
[postimg setImageWithURL:[NSURL URLWithString:@"url link"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]
completed:^(UIImage *image, NSError *err, SDImageCacheType typ) {
/* 等待圖片載完後才addImage */
[controller addImage:postimg.image];
}];
這樣就完成了耶! 分享給需要的人.
下面的link還有twitter的範例, 相當清晰易懂!
參考資料:http://www.appcoda.com/ios-programming-101-integrate-twitter-and-facebook-sharing-in-ios-6/
2013年8月12日 星期一
[Android] 使用Google Map with V2 API
1. 確認下載並配置好Google Play services SDK
Window > Android SDK Manager
1. ) 要把library import進來
3. 在Application的Manifest檔裡作設定
接下來就是把 Key 放進應用程式中.打開 AndroidManifest.xml 檔, 選擇 Application Tab,Add -> Meta Data, 在 Name 欄位填入 "com.google.android.maps.v2.API_KEY", 在 Value 欄位填入剛才複製的 API Key 。
選擇 Permissions Tab, Add -> Permission, 在 Name 欄位填入"

題外話: 透過這個Google Play services APK 似乎有與server端連線, 否則怎麼能update new APIs?
再說連線方式猜測應該是類似推播方式, 註冊完等需要update時喚起手機端service APK對應function到server update?
如果Google Play services APK不大改interface, 開發者應該不會太痛苦才是
又發現Google Play services囊括Google+, map等服務, 這會不會很痴肥阿!?
不過這些服務都已經整合OAuth2相當方便阿
運作方式如下圖

1. ) 要把library import進來

2. ) project就在當初ADT下載回來的資料夾底下
(/sdk/extras/google/google_play_service)
(/sdk/extras/google/google_play_service)

3. ) import 進來的GooglePlay library 記得要build過!
確定在google_play_services_lib/bin/google-play-services_lib.jar是存在的
不然我們用到google map的專案會發生import path的error!
4. ) Build Target要記得換成Google APIs喔!!
確定在google_play_services_lib/bin/google-play-services_lib.jar是存在的
不然我們用到google map的專案會發生import path的error!
4. ) Build Target要記得換成Google APIs喔!!

2. 取得API key
1. ) 左側欄 > Services

Note: The Google Maps Android API v2 uses a new system of managing keys. Existing keys from a Google Maps Android v1 application, commonly known as MapView, will not work with the v2 API.
2. ) SHA1 key也不用開console辛苦尋找了
ADT > Preferences > Android > Build
Bingo! 裡頭的SHA1 fingerprint就是我們要的!

若是真要從console取得
keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android
在填key的時候別忘了尾巴要加上package name! (SHA1 fingerprint);com.example

3. 在Application的Manifest檔裡作設定

選擇 Permissions Tab, Add -> Permission, 在 Name 欄位填入"
com.tonycube.googlemapdemo.permission.MAPS_RECEIVE", 綠色部份請換成你的套件名稱. Protection level 欄位請選擇"signature".
再次 Add -> Uses Permission, 在 Name 欄位填入 "com.tonycube.googlemapdemo.permission.MAPS_RECEIVE", 一樣綠色部份換成你的套件名稱.
4. 把map元件拉進application 開始happy coding吧:)
下面是這版地圖的特色:
- 地圖封裝在 MapFragment 類別中。
- 由於地圖封裝在 MapFragment 類別中,你可以延伸標準的 Activity,而不是切換 MapActivity。
- 使用vector tiles技術可以使地圖顯示得更快,頻寬使用的更少。
- Caching 比較少,所以看到的地圖空白區域會更少。
- 可以顯示3D及透視(室內)地圖。
2013年8月7日 星期三
[Android]開發技巧 靜態常數宣告
Use Static Final For Constants
Consider the following declaration at the top of a class:
static int intVal = 42; static String strVal = "Hello, world!";
The compiler generates a class initializer method, called
, that is executed when the class is first used. The method stores the value 42 into intVal, and extracts a reference from the classfile string constant table forstrVal. When these values are referenced later on, they are accessed with field lookups.
We can improve matters with the "final" keyword:
static final int intVal = 42; static final String strVal = "Hello, world!";The class no longer requires a
method, because the constants go into static field initializers in the dex file. Code that refers to intVal will use the integer value 42 directly, and accesses to strVal will use a relatively inexpensive "string constant" instruction instead of a field lookup.
靜態常數宣告 盡可能使用 Final以減少 field lookup的動作, 而是直接參照 "字串常數".
[Android] ADT 開發環境 語系設定
緣起
熊熊踏入Android的地獄殿堂
import專案進來發現.java檔裡面中文字都變成亂碼

實作
要解決這個問題, 從ADT > 偏好設定 > General > Workspace
將Default 改成utf-8即可

還不行喔?!
那不然再看一下project設定
project右鍵 > Properties > Resource
不從預設container繼承, 再看看中文字是不是都正常了呢 :)

[Android] 讓ADT看起來養眼一點
緣起
摸了幾天Android讓我非常不能適應的一個地方就是編輯器是白底的,
只要是寫程式的人都能體會這種眼睛要爆掉的感覺@@~
整天看著一台27"螢幕, 字又小小的, 眼睛真的看到都快瞎了!
不信嗎!? 寫xcode的時候:
Eclipse - ADT長這樣

實作
下面講解一下調整的步驟:
1. Help > Install New Software...

確認 Eclipse Color Theme 選項有勾選 > 下方的 'Next'

3. 依序 '下一步' '同意licenses' 完成安裝後會重啟Eclipse - ADT > 偏好設定
General > Appearance > Color Theme

如果要調整字體樣式大小, 下面的Colors and Fonts標籤裡面可以修改

結論
調完舒服多啦~ 人客阿, 你說是不是:)

訂閱:
文章 (Atom)
















