2010年12月5日星期日

Activity orientation | Activity Restart

The values of Orientation are ORIENTATION_PORTRAIT and
ORIENTATION_LANDSCAPE.


When orientation is changed, the default action is to restart the Activtiy.

If you don’t want your Activity to restart, you should do the following thing:


Config your Activity with the Activity Element
     android:configChanges="keyboardHidden|orientation"
Rewrite the callback method
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (this.getResources().getConfiguration().orientation ==
    Configuration.ORIENTATION_LANDSCAPE) {
    Log.d(TAG, "++ ORIENTATION_LANDSCAPE ++");
   
    } else if (this.getResources().getConfiguration().orientation
    == Configuration.ORIENTATION_PORTRAIT){
    Log.d(TAG, "++ ORIENTATION_PORTRAIT ++");
    }
}

2010年12月2日星期四

Android Bluetooth

Android Bluetooth

Bluetooth is a communication protocol designed for short-range, low-bandwidth, peer-to-peer communications.
Bluetooth devices and connections are only managed by the following classes:
l         BluetoothAdapter local Bluetooth adapter
l         BluetoothDevice remote Bluetooth device
l         BluetoothSocket
l         BluetoothServerSocket

Accessing the local BT adapter

BluetoothAdapter mBTAdapter = BluetoothAdapter.getDefaultAdapter();
In order to access the local BT adapter, you should include the Bluetooth manifest permission.
"android.permission.BLUETOOTH" />
"android.permission.BLUETOOTH_ADMIN" />

Managing BT Properties and State

The most useful properties are friendly name and MAC address:
mBTAdapter.getName()
mBTAdapter.getAddress()
The current BT Adpater’s state:
l         STATE_TURNING_ON
l         STATE_ON
l         STATE_TURNING_OFF
l         STATE_OFF

Turning on local BT Adapter

private static final int RQUEST_ENABLE_BT = 0;
Intent enalbeIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, RQUEST_ENABLE_BT);
A sub-activity will be pop-up. The result code of the sub-activity will be returned on the callback method onActivityResult(int requestCode, int resultCode, Intent data).

Being discoverable

The BT Discoverability is indicated by the scan mode.
There are three scan modes (getScanMode()).
SCAN_MODE_CONNECTABLE_DISCOVERABLE
SCAN_MODE_CONNECTABLE
SCAN_MODE_NONE
To turn on discovery you need to obtain permission from the user by starting an Activity:
String dis = BluetoothAdapter.ACTION_RQUEST_DISCOVERABLE;
Intent mDis = new Intent(dis);
StartActivityForResult(mDis, DISCOVERY_REQUEST);
To start discovery process, you should call function
mAdapter.startDiscovery();
To cancel discovery process, you should call function
mAdapter.cancelDiscovery();
To get the remote bluetooth device discovered, you should use the BroadcastReceiver,
registerReceiver(discoveryResult, new IntentFilter(BluetoothDevice.ACTION_FOUND));
BroadcastReceiver discoveryResult = new BroadcastReceiver(){
public void onReceiver(Context ct, Intent intent){
   String mRemoteDeviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
   BluetoothDevice mRemoteDevice;
   mRemoteDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
}
}

Bluetooth Communications

The communication channel is a RFCOMM channel.
To establish the bidirectional communications, you should use the following classes:
l         BluetoothServerSocket  A bluetooth server socket is used to listen for incoming Bluetooth Socket connection request from remote Bluetooth Devices.
l         BluetoothSocket
The server socket is created by listenUsingRfcommWithServiceRecord method on mAdapter. The server socket is identified by a name and a UUID.
To start listening for connections call accept on the server.

Transmitting Data Using Bluetooth Sockets

Once a channel is established, the two peers can talk with each other by BluetoothSocket.

2010年11月7日星期日

Set Git Proxy


## Set a proxy for curl
$ export http_proxy="http://proxy.bupt.edu.cn:8080"
## Get the connect.c file and compile it
$ curl http://www.meadowy.org/~gotoh/ssh/connect.c > connect.c
$ gcc -o connect connect.c
## Write a bash file
$ cd ~/bin
$ vi http-proxy
    #!/bin/sh
    # FileName: http-proxy
    # This connects to an HTTP Proxy using connect.c
    connect -H http://proxy.bupt.edu.cn:8080 $@
$ chmod a+x http-proxy
## Run git config and you should change the name "kernel.org" to your interest site.
$ git config --global core.gitproxy "http-proxy for kernel.org"
## If your proxy needs an auth, write rc file for connect.
$ vi /etc/connectrc
    HTTP_PROXY_USER=useraccount
    HTTP_PROXY_PASSWORD=passwd

网络问题的检查 - Linux Network Check


One day, I encountered a problem that the STB had got the IP address, but it could not access the internet.
So, I decided to check the following:
1. DNS Configuration
cat /etc/resolv.conf

gateway 10.192.208.254
nameserver 10.194.128.15

2. Route Table

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.1.0     *               255.255.255.0   U     0      0        0 eth0

You see, the Gateway was wrong. So, I used the command route to add a default gateway.
/sbin/route add default gw 192.168.1.1
3. Ping www.baidu.com. Ok. It worked.

2010年10月7日星期四

多个Gtalk实例同时运行

Gtalk是可以多实例运行的,方法如下:
1. 创建Gtalk的桌面快捷方式
2. 右键选择属性,选择快捷方式标签页
3. 修改Target一栏,如下:
"C:\Program Files\Google\Google Talk\googletalk.exe" /nomutex

2009年12月29日星期二

Public a git repository

Create an empty repository

$ mkdir Andcp_K29

$ cd Andcp_K29

$ git-init-db

Modify .git/info/exclude

$ echo "*.[oa]" >> exclude

$ echo ".svn" >> exclude

$ echo ".repo" >> exclude

Add files to repository

$ git add .

$ git commit -m "Initial repo for example"

Create a public repository

$ mkdir /var/www/html/Andcp_K29.git

$ GIT_DIR=Andcp_K29.git git-init-db

$ git push :/var/www/html/Andcp_K29.git

$ git-daemon --verbose --export-all --enable=receive-pack --base-path=/var/www/html/git &

Run Test

$ git clone git://127.0.0.1/Andcp_K29.git

OK!

2009年12月27日星期日

Config Android Proxy

$ adb shell
# sqlite3 /data/data/com.android.providers.settings/databases/settings.db
sqlite> INSERT INTO system VALUES(99,’http_proxy', 'wwwgate0.mot.com:1080');
sqlite> exit