2012年9月20日 星期四
How to mount jffs2 on linux
2011年10月4日 星期二
Linux 底下好用的文字編輯器-Geany
最近因為寫QT的,所以發現QtCreater也不錯用,引導功能也不錯!又有人推Geany裝了一下覺得不錯用更像UltraEdit功能不錯!
在ubuntu下安裝udo apt-get install geany
在windows也發現一套不用錯,又不比UltraEdit差的軟體"PSPad" - 功能超強的免費編輯器
http://www.pspad.com/en/download.php可以下載到,也是要推一下!
所以整理兩套文字編輯器
1.linux 下geany網址https://launchpad.net/%7Egeany-dev/+archive/ppa
2.在windows下PSPadEdit網址在http://www.pspad.com/en/download.php
可以提供作為使用上的參考。
2011年1月23日 星期日
移植IPv6
看來IPv4與IPv6在Socket軟體的寫作上有些不太一樣的地方。
1. gethostbyname(argv[1]);是在IPv4上使用。
gethostbyname2(argv[1], AF_INET6);是在IPv6上使用。
2. struct sockaddr_in server; 是在IPv4上使用的結構
struct sockaddr_in6 server;是在IPv6上使用的結構
3.細部分的不同就參考下面的程式吧!
4.如果軟體在寫作上有明顯的不同參數設定,即表示協構上相同,但是指令上應該是不相容吧!
Porting IPv6 -- examples
Consider the following IPv4 code examples:
IPv4 client code
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <netdb.h>
...
main(argc, argv) /* client side */
int argc;
char *argv[];
{
struct sockaddr_in server;
struct servent *sp;
struct hostent *hp;
int s;
...
sp = getservbyname("login", "tcp");
if (sp == NULL) {
fprintf(stderr, "rlogin: tcp/login: unknown service\n");
exit(1);
}
hp = gethostbyname(argv[1]);
if (hp == NULL) {
fprintf(stderr, "rlogin: %s: unknown host\n", argv[1]);
exit(2);
}
memset((char *)&server, 0, sizeof(server));
memcpy((char *)&server.sin_addr, hp->h_addr, hp->h_length);
server.sin_len = sizeof(server);
server.sin_family = hp->h_addrtype;
server.sin_port = sp->s_port;
s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0) {
perror("rlogin: socket");
exit(3);
}
...
/* Connect does the bind for us */
if (connect(s, (struct sockaddr *)&server, sizeof(server)) < 0) {
perror("rlogin: connect");
exit(5);
}
...
exit(0);
}
IPv4 server code
#include <sys/types.h>This code can be ported to IPv6 with only a small number of changes.
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <netdb.h>
...
main(argc, argv) /* server side */
int argc;
char *argv[];
{
int f;
struct sockaddr_in from;
struct sockaddr_in sin;
struct servent *sp;
sp = getservbyname("login", "tcp");
if (sp == NULL) {
fprintf(stderr,
"rlogind: tcp/login: unknown service\n");
exit(1);
}
...
#ifndef DEBUG
/* Disassociate server from controlling terminal. */
...
#endif
memset((char *)&sin, 0, sizeof(sin));
sin.sin_len = sizeof(sockaddr_in);
sin.sin_port = sp->s_port; /* Restricted port */
sin.sin_addr.s_addr = INADDR_ANY;
...
f = socket(AF_INET, SOCK_STREAM, 0);
...
if (bind(f, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
...
}
...
listen(f, 5);
for (;;) {
int g, len = sizeof(from);
g = accept(f, (struct sockaddr *) &from, &len);
if (g < 0) {
if (errno != EINTR)
syslog(LOG_ERR, "rlogind: accept: %m");
continue;
}
if (fork() == 0) {
close(f);
doit(g, &from);
}
close(g);
}
exit(0);
}
These changes are highlighted in the examples below by comments in the code.
IPv4 client code ported to IPv6
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <netdb.h>
...
main(argc, argv) /* client side */
int argc;
char *argv[];
{
/* */
/* OLD code: struct sockaddr_in server; */
/* */
/* Change structure to sockaddr_in6 from sockaddr_in. */
/* */
struct sockaddr_in6 server;
struct servent *sp;
struct hostent *hp;
int s;
...
sp = getservbyname("login", "tcp");
if (sp == NULL) {
fprintf(stderr, "rlogin: tcp/login: unknown service\n");
exit(1);
}
/* */
/* OLD code: hp = gethostbyname(argv[1]); */
/* */
/* Use gethostbyname2 instead of gethostbyname. */
/* */
hp = gethostbyname2(argv[1], AF_INET6);
if (hp == NULL) {
fprintf(stderr, "rlogin: %s: unknown host\n", argv[1]);
exit(2);
}
memset((char *)&server, 0, sizeof(server));
/* */
/* OLD code: Not applicable. */
/* */
/* If the len member was not in the original IPv4 code*/
/* add it now and make sure it is sin6_len for IPv6. */
/* */
server.sin6_len = sizeof(server);
/* */
/* OLD code: memcpy((char *)&server.sin_addr, ... */
/* OLD code: server.sin_family = hp->h_addrtype; */
/* OLD code: server.sin_port = sp->s_port; */
/* */
/* Make sure you are using sockaddr_in6 members. */
/* */
memcpy((char *)&server.sin6_addr, hp->h_addr, hp->h_length);
server.sin6_family = hp->h_addrtype;
server.sin6_port = sp->s_port;
/* */
/* OLD code: s = socket(AF_INET, SOCK_STREAM, 0); */
/* */
/* Use the correct address family for IPv6. */
/* */
s = socket(AF_INET6, SOCK_STREAM, 0);
if (s < 0) {
perror("rlogin: socket");
exit(3);
}
...
/* Connect does the bind for us */
if (connect(s, (struct sockaddr *)&server, sizeof(server)) < 0) {
perror("rlogin: connect");
exit(5);
}
...
exit(0);
}
NOTE: In the assignments to
server.sin6_addr
andserver.sin6_family
hp->h_length
will always beequal to sizeof(struct in6addr) and
hp->h_addrtype
willalways be equal to AF_INET6.
IPv4 server code ported to IPv6
#include <sys/types.h>As can be seen in the two IPv6 ported examples, there are only a few
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <netdb.h>
...
main(argc, argv) /* server side */
int argc;
char *argv[];
{
int f;
/* */
/* OLD code: struct sockaddr_in from; */
/* OLD code: struct sockaddr_in sin; */
/* */
/* Change structure to sockaddr_in6 from sockaddr_in. */
/* */
struct sockaddr_in6 from;
struct sockaddr_in6 sin;
struct servent *sp;
sp = getservbyname("login", "tcp");
if (sp == NULL) {
fprintf(stderr,
"rlogind: tcp/login: unknown service\n");
exit(1);
}
...
#ifndef DEBUG
/* Disassociate server from controlling terminal. */
...
#endif
memset((char *)&sin, 0, sizeof(sin));
/* */
/* OLD code: Not applicable. */
/* */
/* If the len member was not in the original IPv4 code*/
/* add it now and make sure it is sin6_len for IPv6. */
/* */
sin.sin6_len = sizeof(sin);
/* */
/* OLD code: sin.sin_port = sp->s_port; */
/* */
/* Make sure you are using sockaddr_in6 members. */
/* */
sin.sin6_port = sp->s_port; /* Restricted port */
/* */
/* OLD code: sin.sin_addr.s_addr = INADDR_ANY; */
/* */
/* Make the modifications for assigning in6addr_any to*/
/* sin6_addr. */
/* */
sin.sin6_addr = in6addr_any;
...
/* */
/* OLD code: f = socket(AF_INET, SOCK_STREAM, 0); */
/* */
/* Use the correct address family for IPv6. */
/* */
f = socket(AF_INET6, SOCK_STREAM, 0);
...
if (bind(f, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
...
}
...
listen(f, 5);
for (;;) {
int g, len = sizeof(from);
g = accept(f, (struct sockaddr *) &from, &len);
if (g < 0) {
if (errno != EINTR)
syslog(LOG_ERR, "rlogind: accept: %m");
continue;
}
if (fork() == 0) {
close(f);
doit(g, &from);
}
close(g);
}
exit(0);
}
changes required to port IPv4 applications to IPv6. You may want to go one step
further and use the new getaddrinfo(3N)
and getnameinfo(3N)
functions to make your IPv6 application more portable. The following examples
show how you could modify the client and server examples to use getaddrinfo(3N).
IPv6 client code using getaddrinfo
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <netdb.h>
main(argc, argv) /* client side */
int argc;
char *argv[];
{
struct addrinfo req, *ans;
int code, s;
req.ai_flags = 0;
req.ai_family = PF_INET6; /* Same as AF_INET6. */
req.ai_socktype = SOCK_STREAM;
/* */
/* Use default protocol (in this case tcp) */
/* */
req.ai_protocol = 0;
if ((code = getaddrinfo(argv[1], "login", &req, &ans)) != 0) {
fprintf(stderr, "rlogin: getaddrinfo failed code %d\n",
code);
exit(1);
}
/* */
/* ans must contain at least one addrinfo, use */
/* the first. */
/* */
s = socket(ans->ai_family, ans->ai_socktype, ans->ai_protocol);
if (s < 0) {
perror("rlogin: socket");
exit(3);
}
...
/* Connect does the bind for us */
if (connect (s, ans->ai_addr, ans->ai_addrlen) < 0) {
perror("rlogin: connect");
exit(5);
}
...
/* */
/* Free answers after use */
/* */
freeaddrinfo(ans);
/* ... */
exit(0);
}
IPv6 server code using getaddrinfo
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <netdb.h>
main(argc, argv) /* server side */
int argc;
char *argv[];
{
struct sockaddr_in6 from;
struct addrinfo req, *ans;
int code, f, len;
/* */
/* Set ai_flags to AI_PASSIVE to indicate that return */
/* address is suitable for bind() */
/* */
req.ai_flags = AI_PASSIVE;
req.ai_family = PF_INET6; /* Same as AF_INET6. */
req.ai_socktype = SOCK_STREAM;
req.ai_protocol = 0;
if ((code = getaddrinfo(NULL, "login", &req, &ans)) != 0) {
fprintf(stderr, "rlogind: getaddrinfo failed code %d\n",
code);
exit(1);
}
...
#ifndef DEBUG
/* Disassociate server from controlling terminal. */
...
#endif
/* */
/* ans must contain at least one addrinfo, use */
/* the first. */
/* */
f = socket(ans->ai_family, ans->ai_socktype, ans->ai_protocol);
...
if (bind(f, ans->ai_addr, ans->ai_addrlen) < 0) {
...
}
listen(f, 5);
for (;;) {
int g, len = sizeof(from);
g = accept(f, (struct sockaddr *) &from, &len);
if (g < 0) {
if (errno != EINTR)
syslog(LOG_ERR, "rlogind: accept: %m");
continue;
}
if (fork() == 0) {
close(f);
doit(g, &from);
}
close(g);
}
/* */
/* Free answers after use */
/* */
freeaddrinfo(ans);
exit(0);
}
2011年1月12日 星期三
玩玩Android的Sensor
其實可以用的sensor還不少
TYPE_ACCELEROMETER :加速感測器
TYPE_ALL :使用所有的感測器
TYPE_GRAVITY :重力感測器
TYPE_GYROSCOPE :陀螺儀傳感器
TYPE_LIGHT :亮度感測器
TYPE_LINEAR_ACCELERATION:線性加速感測器
TYPE_MAGNETIC_FIELD 磁場感測器
TYPE_ORIENTATION : 方向傳感器
TYPE_PRESSURE:壓力感測器
TYPE_PROXIMITY:接近感測器
TYPE_ROTATION_VECTOR 旋轉向量傳感器
TYPE_TEMPERATURE:溫度感測器
不過在的我HTC上溫度感測器跟壓力感測器這兩個東西好像沒有什麼作用!
2011年1月10日 星期一
買了HTC DHD開始玩Android
1.今天玩讓視窗固定不會,轉來轉去設定的方法是在AndroidManifest.xml
裡加入
android:screenOrientation="landscape"
android:configChanges="orientation|keyboardHidden"
就可以固定視窗了!
2 LinearLayout這個元件主要是一行一個元件。通常要和並LinearLayout.LayoutParams 的參數一起使用
也就是說用LinearLayout.LayoutParams來設定元件的大小,元件和元件間的尺吋。
3.取得視窗大小的參數
Display display = getWindowManager().getDefaultDisplay();
int orientation = display.getOrientation(); // 視窗旋轉的角度
int screen_width = display.getWidth(); //視窗寬度
int screen_height = display.getHeight();//視窗高度
4.整個應用軟體視窗的大小,可以使用Layout,getHeight(),getWidth()來取,
但是如果是在public void onCreate(Bundle savedInstanceState)通常取到的都是0,可能是生命週期的問題,如果是在Button的事件中取,就可以取到正確的值了!而且比視窗的值來的有用。
2010年11月3日 星期三
Intent 用法大公開[轉]
How to use Intent to send an email, SMS, open a web browser, show map, etc.?
Intent 應該算是 Andorid 中特有的東西。你可以在 Intent 中,指定要應用程式執行的動作 (view, edit, dial),以及應用程式執行該動作時,所需要的資料。都指定好後,只要透過 startActivity(),Android 系統會自動尋找,最符合你指定要求的應用程式,並喚起執行該應用程式。
不過,這部份的文件還不是很完整。Reference of Available Intents 有列一些。底下是我收集的一些用法,分享出來給有需要的你。有些還沒有實際驗證過,如果發現有錯誤,或有新的用法,也請告訴我。
顯示網頁
Uri uri = Uri.parse("http://google.com");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
顯示地圖
Uri uri = Uri.parse("geo:38.899533,-77.036476");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
//其他 geo URI 範例
//geo:latitude,longitude
//geo:latitude,longitude?z=zoom
//geo:0,0?q=my+street+address
//geo:0,0?q=business+near+city
//google.streetview:cbll=lat,lng&cbp=1,yaw,,pitch,zoom&mz=mapZoom
路徑規劃
Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
//where startLat, startLng, endLat, endLng are a long with 6 decimals like: 50.123456
撥打電話
//叫出撥號程式
Uri uri = Uri.parse("tel:0800000123");
Intent it = new Intent(Intent.ACTION_DIAL, uri);
startActivity(it);
//直接打電話出去
Uri uri = Uri.parse("tel:0800000123");
Intent it = new Intent(Intent.ACTION_CALL, uri);
startActivity(it);
//用這個,要在 AndroidManifest.xml 中,加上
//
傳送 SMS/MMS
//叫起簡訊程式
Intent it = new Intent(Intent.ACTION_VIEW);
it.putExtra("sms_body", "The SMS text");
it.setType("vnd.android-dir/mms-sms");
startActivity(it);
//傳送簡訊
Uri uri = Uri.parse("smsto:0800000123");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", "The SMS text");
startActivity(it);
//傳送 MMS
Uri uri = Uri.parse("content://media/external/images/media/23");
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra("sms_body", "some text");
it.putExtra(Intent.EXTRA_STREAM, uri);
it.setType("image/png");
startActivity(it);
//如果是 HTC Sense 手機,你要用
Intent sendIntent = new Intent("android.intent.action.SEND_MSG");
sendIntent.putExtra("address", toText);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "subject");
sendIntent.putExtra("sms_body", textMessage);
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(url));
sendIntent.setType("image/jpeg");
startActivity(sendIntent);
//底下這段更好,可在所有手機上用
//refer to http://stackoverflow.com/questions/2165516/sending-mms-into-different-android-devices
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mmsto:
intent.putExtra("address",
intent.putExtra("subject",
startActivity(intent);
傳送 Email
Uri uri = Uri.parse("mailto:xxx@abc.com");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(it);
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_EMAIL, "me@abc.com");
it.putExtra(Intent.EXTRA_TEXT, "The email body text");
it.setType("text/plain");
startActivity(Intent.createChooser(it, "Choose Email Client"));
Intent it=new Intent(Intent.ACTION_SEND);
String[] tos={"me@abc.com"};
String[] ccs={"you@abc.com"};
it.putExtra(Intent.EXTRA_EMAIL, tos);
it.putExtra(Intent.EXTRA_CC, ccs);
it.putExtra(Intent.EXTRA_TEXT, "The email body text");
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it.setType("message/rfc822");
startActivity(Intent.createChooser(it, "Choose Email Client"));
//傳送影音附件檔
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/mysong.mp3"));
it.setType("audio/mp3");
startActivity(Intent.createChooser(it, "Choose Email Client"));
//傳送圖片附件檔
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/mypic.jpg"));
it.setType("image/jpeg");
startActivity(Intent.createChooser(it, "Choose Email Client"));
顯示聯絡人清單
Intent it = new Intent(Intent.ACTION_VIEW, People.CONTENT_URI);
startActivity(it);
顯示某個朋友的詳細資料
Uri uriPerson = ContentUris.withAppendedId(People.CONTENT_URI, 5); //5 是朋友的 ID
Intent it = new Intent(Intent.ACTION_VIEW, uriPerson);
startActivity(it);
播放多媒體
Intent it = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/song.mp3");
it.setDataAndType(uri, "audio/mp3");
startActivity(it);
Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
從圖庫中回傳選到的圖片
Intent it = new Intent(Intent.ACTION_GET_CONTENT);
it.addCategory(Intent.CATEGORY_OPENABLE);
it.setType("image/*");
startActivityForResult(it, 0);
//回傳的圖片可透過 it.getData() 取得圖片之 Uri
啟動照相機,並將相片存在指定的檔案中
Intent it = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//假設你要將相片存在 /sdcard/xxx.jpg 中
File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/xxx.jpg");
it.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivity(it, 0);
Market 相關
//尋找某個應用程式
Uri uri = Uri.parse("market://search?q=pname:pkg_name");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
//where pkg_name is the full package path for an application
//顯示某應用程式詳細畫面
Uri uri = Uri.parse("market://details?id=pkg_name_or_app_id");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
//where app_id is the application ID, find the ID
//by clicking on your application on Market home
//page, and notice the ID from the address bar
Uninstall 應用程式
Uri uri = Uri.fromParts("package", strPackageName, null);
Intent it = new Intent(Intent.ACTION_DELETE, uri);
startActivity(it);
安裝 APK 檔
Uri uri = Uri.parse("url_of_apk_file");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
it.setData(uri);
it.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
it.setClassName("com.android.packageinstaller",
"com.android.packageinstaller.PackageInstallerActivity");
startActivity(it);
//make sure the url_of_apk_file is readable for all users