RSS
 

Posts Tagged ‘Programming’

iPhone 開發筆記 – Icons 篇

08 Jan

當我一開始開發 iPhone 時, 對於 icons 大小完全沒概念. 我本身也比較懶得去讀 iPhone SDK, 所以一路走來都是這樣誤打誤撞. 下列是我整理出來的小筆記. 也讓有興趣開發iPhone程式的入門者有一些概念.

上傳到 App Store 的 Icon 要準備的大小如下 :

512×512(實際會縮小到 175×175 放在 app store 上)

舊版 iPhone (3G/3GS) Icon 如下 :

57×57

iPad icon 如下 :

72×72

iPhone 4 或 retina icon 如下 :

114×114

以上 icons 準備好之後, 在你的 xxxx-Info.plist 檔案加入 Icons 的內容, 分別描述不同大小的檔案名稱

這樣你的程式就可以支援 iPhone / iPad 還有 retina 螢幕了

 

[php] plurk bot 跨年撲

31 Dec

先前看到 皮樂的撲. 有人傳說會加業障100. 所以來玩看看吧

材料 :
Linux base機器一台(mac也許可以), at, php 還有 php plurk api,

烹煮方式 :
1. 先確認你的 linux 環境有沒有 at 和 php 指令.
2. 下載 php plurk api , 放到你想執行的目錄.
3. 到 Get an API key 申請 plurk api key.
4. 撰寫簡單的撲機器人, 該檔案放在與 php plurk api 同目錄. 下面是簡單的撲

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/php
<?php
 
$api_key = 'YOUR_PLURK_API_KEY';
$username = 'YOUR_PLURK_ID';
$password = 'YOUR_PLURK_PASSWORD';
 
require('plurk_api.php');
 
$plurk = new plurk_api();
$plurk->login($api_key, $username, $password);
 
$output = "新年快樂..林北現在正在拍煙火...";
echo $output;
echo "\n\n ----- add plurk ----- \n";
$plurk->add_plurk('en', 'says', $output);
?>

5. 存檔後用 php 指令去執行它. 理論上它就可以幫你自動撲到你的 plurk. 如下 :

/usr/bin/php /home/sam/plurk/newyear.php

可以把這個指令存到文字檔內. 我命名它為 plurk_newyear

6. 再搭配 at 指令讓他在凌晨的時候撲.

# at 00:00 < plurk_newyear

7. 檢查一下看看它是不是在排程當中.

# at -l
1 2011-01-01 00:00 a sam

順利的話..就會有跨年撲. XD

 
 

註冊 iPhone 多工背景播放音樂

14 Dec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
 
    // Override point for customization after application launch.
    [self.window addSubview:tabBarController.view];
    [self.window makeKeyAndVisible];
 
	UIDevice *thisDevice = [UIDevice currentDevice];
 
	if([thisDevice respondsToSelector:@selector(isMultitaskingSupported)]
	   && thisDevice.multitaskingSupported) {
		UIBackgroundTaskIdentifier backgroundTask = [application beginBackgroundTaskWithExpirationHandler:^{
			/* just fail if this happens. */
			NSLog(@"BackgroundTask Expiration Handler is called");
			[application endBackgroundTask:backgroundTask];
		}];
	}
 
    return YES;
}
 
 

HOWTO Delete Non-primary Google Calendar Event?

11 Oct

When you created google calendar event, you will get google event id. something like below :

http://www.google.com/calendar/feeds/default/private/full/yyyyy

After you created non-primary google calendar event, you will get event id similar with following :

http://www.google.com/calendar/feeds/liho.tw_xxxxx%40group.calendar.google.com/

private/full/yyyyy

Following is part of Calendar.php which only supports the google default event :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function deleteEventById ($client, $eventId)
{
  $event = getEvent($client, $eventId);
  $event->delete();
}
 
function getEvent($client, $eventId)
{
  $gdataCal = new Zend_Gdata_Calendar($client);
  $query = $gdataCal->newEventQuery();
  $query->setUser('default');
  $query->setVisibility('private');
  $query->setProjection('full');
  $query->setEvent($eventId);
 
  try {
    $eventEntry = $gdataCal->getCalendarEventEntry($query);
    return $eventEntry;
  } catch (Zend_Gdata_App_Exception $e) {
    var_dump($e);
    return null;
  }
}

You need to un-commet the following :

$query->setUser(‘default’);

And add your calendar id to similar below :

$query->setUser(‘liho.tw_xxxxx%40group.calendar.google.com’);

After you modified, you are able to delete non-primary google calendar event. However, above example is not smart enough. You should rewrite the getEvent function to add non-primary google calendar id. like below :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function deleteEventById ($client, $eventId, $calendarid = "default")
{
  $event = getEvent($client, $eventId, $calendarid);
  $event->delete();
}
 
function getEvent($client, $eventId, $calendarid)
{
  $gdataCal = new Zend_Gdata_Calendar($client);
  $query = $gdataCal->newEventQuery();
  $query->setUser($calendarid);
  $query->setVisibility('private');
  $query->setProjection('full');
  $query->setEvent($eventId);
 
  try {
    $eventEntry = $gdataCal->getCalendarEventEntry($query);
    return $eventEntry;
  } catch (Zend_Gdata_App_Exception $e) {
    var_dump($e);
    return null;
  }
}
 
 

Android – 機房溫度監測簡單範例

01 Sep

分享一個簡單的 android 程式範例. 主要用 ImageView 顯示某 URL 的圖片. 這算是我第一隻比較有用處的 Android 程式


將手機連接到電腦. 啟用 debug 模式. 執行之後就可以將程式部屬到 android 手機了. 這比起蘋果的 Xcode 方便許多. iphone 系列手機沒破解的話就不能直接在設備上執行. 真是有點令人討厭.


執行 Temperature 程式結果

以下為 Android 各檔案的內容 :
AndroidManifest.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="tw.edu.sinica.tiara.temperature"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Temperature"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
 
    </application>
    <uses-sdk android:minSdkVersion="5" />
 
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>

在這裡要注意是否加入 <uses-permission android:name=”android.permission.INTERNET”></uses-permission>. 如果沒加入的話.程式不能存取 internet. 這個我搞了好久才想起來要這樣弄.

下面檔案主要在描述 UI 部分
layout/main.xml

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:text="@string/title" android:gravity="center" android:textSize="16sp" android:textColor="#FF000000" android:background="#FFFFFFFF"/>
<ImageView android:id="@+id/ImageViewR937" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FFFFFFFF"></ImageView>
</LinearLayout>

下列檔案在描述 strings
values/strings.xml

1
2
3
4
5
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="title">R937</string>
    <string name="app_name">Temperature</string>
</resources>

以下為程式主要的檔案 :
Temperature.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package tw.edu.sinica.tiara.temperature;
 
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
 
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
 
public class Temperature extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        ImageView imgView = (ImageView)findViewById(R.id.ImageViewR937);
        Drawable drawable = ImageOperations("http://somewhere/monitoring/temp.jpg");
        imgView.setImageDrawable(drawable);
    }
 
    private Drawable ImageOperations(String url) {
		try {
			InputStream is = (InputStream) new URL(url).getContent();
			Drawable d = Drawable.createFromStream(is, "src name");
			return d;
		} catch (MalformedURLException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
 
}

這樣算是勉強能用的程式. 這還需要在改進. 例如, 使用 Timer 定時讀溫度. 然後設定超過多少溫度顯示 Alert. 當然, 要弄個 Setting 介面讓使用者定義 interval & alert temperature. 之後, 在慢慢加入. 暫時先這樣!

 

精省錢 將字串複製到記憶體筆記

02 Aug
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
if ([host isEqualToString:@"mympro_Copy"]) {
    NSString *command = request.URL.path.lastPathComponent;
    if ([command isEqualToString:@"mobileNumber"]) {
        NSString *mobileNumber = [[NSUserDefaults standardUserDefaults] stringForKey:@"mobileNumber"];
        if (mobileNumber.length != 0) {
            [UIPasteboard generalPasteboard].string = mobileNumber;
            [labelStatus setText:@"門號已複製到記憶體..."];
        } else {
            [labelStatus setText:@"尚未設定門號..."];
        }
    }
    if ([command isEqualToString:@"mobilePasswd"]) {
        NSString *mobilePasswd = [[NSUserDefaults standardUserDefaults] stringForKey:@"mobilePasswd"];
        if (mobilePasswd.length != 0) {
            [UIPasteboard generalPasteboard].string = mobilePasswd;
            [labelStatus setText:@"密碼已複製到記憶體..."];
        } else {
            [labelStatus setText:@"尚未設定密碼..."];
        }
    }
    return NO;
}
 
- (IBAction)btnNumberClicked:(id)sender {
	NSString *mobileNumber = [[NSUserDefaults standardUserDefaults] stringForKey:@"mobileNumber"];
 
	if (mobileNumber.length != 0) {
		[UIPasteboard generalPasteboard].string = mobileNumber;
		[labelStatus setText:@"門號已複製到記憶體..."];
	} else {
		[labelStatus setText:@"尚未設定門號..."];
	}
}
 
- (IBAction)btnPasswdClicked:(id)sender {
	NSString *mobilePasswd = [[NSUserDefaults standardUserDefaults] stringForKey:@"mobilePasswd"];
 
	if (mobilePasswd.length != 0) {
		[UIPasteboard generalPasteboard].string = mobilePasswd;
		[labelStatus setText:@"密碼已複製到記憶體..."];
	} else {
		[labelStatus setText:@"尚未設定密碼..."];
	}
}
 
 

My fone – 台灣大哥大節費程式測試

29 Mar


程式名稱為My fone


載入畫面


操作畫面.

請台灣大哥大的朋友幫忙到 http://liho.tw/fone/測試.
我認為應該有很多錯誤. 因為我不熟台灣大哥大的規則.
如果您知道有計算上的錯誤請您告訴我.謝謝.

有點小累, 先這樣.晚點再補齊.

 

php搜尋中文字

28 Mar
1
2
3
4
5
6
7
<?
mb_internal_encoding('UTF-8');
$output =  "123中文測試...";
$pos = mb_strpos($output, "中文");
$output = substr($output, $pos);
echo $output;
?>

上面執行結果為 :

中文測試…

 
 

php用curl抓網頁轉Big5到UTF-8範例

28 Mar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?
$webpage = $_GET["w"];
 
$ch = curl_init();
 
curl_setopt($ch, CURLOPT_URL, $webpage);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
$output = curl_exec($ch);
 
curl_close($ch);
 
echo iconv("Big5", "UTF-8", $output);
?>
 
 

My mPro – iPhone 節費小程式

24 Mar

此篇文章為舊的資訊, 請到精省錢官方網頁.

今天晚上臨時想寫個節費程式. 這樣就可以算到底還有多少通簡訊可以傳. 到底還有幾分鐘通話可以使用. 目前這隻程式還算很陽春. 最好是能夠直接登入emome然後把資料撈回來. Orz 就不用輸入那些有的沒有的通話及簡訊費用了.

畫面大概如下 :


程式名稱為My mPro


載入畫面


大家講使用畫面


元氣型使用畫面


基本型使用畫面

請用iPhone瀏覽 : http://liho.tw/mpro/ 頁面. 記得加入主畫面(Home Screen).
在使用這個節費程式前, 先到 emome 註冊你的帳號. 然後進入我的帳單網頁查詢您的未出帳的資訊. 在進去 My mPro 程式計算目前剩餘通話量.