2011年9月12日 星期一

Android學習筆記 - Broadcast

ACTION_BATTERY_CHANGED 充電中
ACTION_BATTERY_LOW 電量低
ACTION_BATTERY_OKAY 電量正常
ACTION_POWER_CONNECTED 充電
ACTION_POWER_DISCONNECTED 拔除充電

ACTION_BOOT_COMPLETED 開機完成
ACTION_REBOOT 重新啟動
ACTION_SHUTDOWN 關機
ACTION_SCREEN_OFF 螢幕關閉
ACTION_SCREEN_ON 螢幕開啟

ACTION_CAMERA_BUTTON Camera按鈕被按

ACTION_HEADSET_PLUG 耳機被插上或拔下

ACTION_CONFIGURATION_CHANGED Configuration(orientation, locale...)改變
ACTION_DATE_CHANGED 日期被改變
ACTION_INPUT_METHOD_CHANGED 輸入法改變
ACTION_LOCALE_CHANGED 地區改變
ACTION_TIMEZONE_CHANGED 時區改變
ACTION_TIME_CHANGED 時間改變
ACTION_TIME_TICK 時間被變更
ACTION_WALLPAPER_CHANGED 背景被變更

ACTION_GTALK_SERVICE_CONNECTED Gtalk連線建立
ACTION_GTALK_SERVICE_DISCONNECTED Gtalk連線中斷

ACTION_NEW_OUTGOING_CALL 播打電話

ACTION_PACKAGE_ADDED 安裝一個新的程式
ACTION_PACKAGE_CHANGED 即有的程式被變更
ACTION_PACKAGE_DATA_CLEARED 程式的資料被清除
ACTION_PACKAGE_FIRST_LAUNCH 程式第一次被執行
ACTION_PACKAGE_RESTARTED 程式被重新執行
ACTION_PACKAGE_INSTALL 安裝程式
ACTION_PACKAGE_REMOVED 程式移除
ACTION_PACKAGE_REPLACED 程式被取代

ACTION_UMS_CONNECTED USB Mount
ACTION_UMS_DISCONNECTED USB Unmount
ACTION_USER_PRESENT wake up


2011年9月7日 星期三

Android學習筆記 - 取得偏好設定(SharedPreferences)

程式中取得偏好設定的方式主要有二個:(假設程式的packageName : tw.nicky)
1. PreferenceManager.getDefaultSharedPreferences(context);
    取得預設的偏好設定,此偏好設定會儲存在
    (/data/data/tw.nicky/shared_prefs/tw.nicky_preferences.xml)

2. context.getSharedPreferences("name", MODE_PRIVATE);
    此偏好設定會儲存在
    (/data/data/tw.nicky/shared_prefs/name.xml)
    第二個參數代表此偏好設定的存取模式
     MODE_PRIVATE : 只有此程式可以存取
     MODE_WORLD_READABLE : 其它程式也可以讀取
     MODE_WORLD_WRITEABLE : 其它程式可以寫入
 
     存取模式也可以混著使用,如:
     MODE_WORLD_READABLE + MODE_WORLD_WRITEABLE : 表示其它程式可讀取寫入

    假設你將存取模式設定為MODE_WORLD_READABLE
    那麼其它的程式,則可透過以下語法來讀取你的偏好設定
    context= createPackageContext("tw.nicky",CONTEXT_IGNORE_SECURITY );
    prefs = myContext.getSharedPreferences(WRITE, MODE_PRIVATE);


2011年6月28日 星期二

將xsd檔轉成Java Code

要將xsd轉成Java只需要使用到xjc這個指令即可,而xjc這個指令就位於jdk下的bin目錄中。

以下指令-p參數表示你要輸出的PackageName

xjc -p your.package.name yourXsdFile.xsd


2010年12月7日 星期二

GAE/J - Appstats(測量App的效能)

Appstats主要是用來測量App在Google App Engine上的效能表現。做法很簡單只要掛上Google提供的Appstats的Filter即可使用。
  • 在web.xml中加入
<filter>
        <filter-name>appstats</filter-name>
        <filter-class>com.google.appengine.tools.appstats.AppstatsFilter</filter-class>
        <init-param>
            <param-name>logMessage</param-name>
            <param-value>Appstats available: /appstats/details?time={ID}</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>appstats</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

  • 連線到http://xxx.appspot.com/appstats/

  • 限制只有管理者可以連線,需在web.xml加入以下的code
<servlet>
        <servlet-name>appstats</servlet-name>
        <servlet-class>com.google.appengine.tools.appstats.AppstatsServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>appstats</servlet-name>
        <url-pattern>/appstats/*</url-pattern>
    </servlet-mapping>

    <security-constraint>
        <web-resource-collection>
            <url-pattern>/appstats/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>admin</role-name>
        </auth-constraint>
    </security-constraint>


2010年12月6日 星期一

Tomcat中限制只有特定IP可以連線

修改在Tomcat目錄下conf\Catalina\localhost\yourApp.xml中的內容:

 以下設定只允許本端電腦和192.168.1.*這個子網路可以進行連線
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/yourApp">
   <Valve className="org.apache.catalina.valves.RemoteHostValve" allow="127.0.0.1,192.168.1.*"/>
</Context>


2010年7月27日 星期二

Android學習筆記 - 模擬器快速鍵

Home鍵 - Home

Back鍵 - Esc

Menu鍵 - F2

撥號鍵 - F3

掛斷鍵 - F4

搜尋鍵 - F5

網絡開關 - F8

旋轉螢幕 - Ctrl+F11


2010年7月14日 星期三

Android學習筆記 - WIFI控制(開啟、關閉)

1. WIFI的控制,主要是透過WifiManager來管理WIFI。

2.WifiManagerExample.java
package tw.nicky.WifiManagerExample;
import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class WifiManagerExample extends Activity {
 private WifiManager wiFiManager;
 private Button turnOnWifiButn;
 private Button turnOffWifiButn;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  // 取得WifiManager
  wiFiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
  turnOnWifiButn = (Button) findViewById(R.id.turnOnWifiButn);
  turnOffWifiButn = (Button) findViewById(R.id.turnOffWifiButn);

  // 開啟wifi
  turnOnWifiButn.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    //若wifi狀態為關閉則將它開啟
    if (!wiFiManager.isWifiEnabled()) {
     wiFiManager.setWifiEnabled(true);
    }
   }
  });

  // 關閉wifi
  turnOffWifiButn.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    //若wifi狀態為開啟則將它關閉
    if (wiFiManager.isWifiEnabled()) {
     wiFiManager.setWifiEnabled(false);
    }
   }
  });
 }
}

3. main.xml(Layout)
<?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"
    >
<Button 
android:text="開啟Wifi" 
android:id="@+id/turnOnWifiButn" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content">
</Button>

<Button 
android:text="關閉Wifi" 
android:id="@+id/turnOffWifiButn" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content">
</Button>
</LinearLayout>
4. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="tw.nicky.WifiManagerExample"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".WifiManagerExample"
                  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-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
    <uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
</manifest> 


5. 執行畫面