2011年8月24日

Android: AlarmManager 鬧鐘用法


Alarm Manager元件用來排程,當時間到時系統會廣播註冊指定的Intent啟動目標程式,就算目標程式並未在執行中也一樣。若是裝置在睡眠中,則依參數而定,註冊的鬧鐘會保留或喚醒裝置。當系統關閉並rebooted時,會清除鬧鐘的設定。當廣播接受端執行onReceive() 時,Alarm Manager會保持CPU清醒直到處理結束。裝置有可能一旦執行完onReceive()時,立刻進入昏睡(睡眠不足:),所以不要在onReceive()中呼叫非同步動作的函數。若一定要作時,請自行處理wake lock policy(鎖定清醒)。
  • ELAPSED_REALTIME 使用 elapsedRealtime() 計時;從系統啟動後至目前的時間(包含睡覺時間)
  • ELAPSED_REALTIME_WAKEUP 同上,並喚醒裝置
  • RTC 使用 currentTimeMillis() 牆壁時鐘UTC格式
  • RTC_WAKEUP  同上,並喚醒裝置

2011年8月23日

Android: Intent 呼叫瀏覽器開啟指定網頁

底下試舉兩種Intent呼叫方法,用兩個Button掛在同一個接收器用getID()區分。
Button.OnClickListener btnCallTestClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
    try {
        String strURL1 = "http://tw.yahoo.com";
        String strURL2 = "http://www.google.com.tw";
        switch (arg0.getId()) {
         case R.id.callTest1:
          Intent ie = new Intent(Intent.ACTION_VIEW,Uri.parse(strURL1));
          startActivity(ie);
          break;
         case R.id.callTest2:
          Intent ie2 = new Intent();
          ComponentName comp = new ComponentName("com.android.browser",
            "com.android.browser.BrowserActivity");
          ie2.setComponent(comp);
          ie2.setAction(Intent.ACTION_VIEW);
          ie2.setData(Uri.parse(strURL2));
          startActivity(ie2);
          break;
        }
        //
     } catch (Exception e) {
        e.printStackTrace();
     }
       
}};

連結: 在 Android 裡讓鬧鐘設定撐過關機(AlarmManager)

Java Artisan: 在 Android 裡讓鬧鐘 Alarm設定撐過關機(AlarmManager)

2011年6月15日

使用內部儲存空間

你可以直接存放檔案在裝置的內部儲存空間,預設存放在內部儲存空間的檔案只有你的程式可以存取。當使用者移除你的程式時,這些檔案也會一併被移除。

寫出檔案
呼叫openFileOutput()傳入檔名和操作模式。傳回一個FileOutputStream。
String FILENAME = "hello_file";
String string = "hello world!";
 
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
MODE_PRIVATE產生或覆蓋檔案,私有
MODE_APPEND增添檔案
MODE_WORLD_READABLE外界可讀取
MODE_WORLD_WRITEABLE外界可寫入

讀取檔案
呼叫openFileInput()傳入檔名,傳回一個FileInputStream。
也可以讀取存放在 res/raw/目錄中的檔案,呼叫openRawResource(),傳入 R.raw. ,傳回一個InputStream(只能讀取無法寫入)。

其他相關函數
getFilesDir()
Gets the absolute path to the filesystem directory where your internal files are saved.
getDir()
Creates (or opens an existing) directory within your internal storage space.
deleteFile()
Deletes a file saved on the internal storage.
fileList()
Returns an array of files currently saved by your application.

RadioButton用法

In AndroidManifest.xml宣告如下:
        ....
        <RadioGroup 
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:checkedButton="@+id/horizontalRB"
            android:gravity="center_horizontal"
        >
          <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/hor"
            android:textColor="@color/black"
            android:id="@id/horizontalRB"          />
          <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:textColor="@color/black"
            android:text="@string/ver"
            android:id="@+id/verticalRB"          />
        RadioGroup>
         ...
在java程式中取值,用Intent帶值putExtra呼叫其他Activity。
        RadioButton rb1=(RadioButton)findViewById(R.id.horizontalRB);
        RadioButton rb2=(RadioButton)findViewById(R.id.verticalRB);        
 
        Intent intent = new Intent();
        if (rb2.isChecked())
            intent.putExtra("vertical",true);
        else
            intent.putExtra("vertical",false);
 
        intent.setClass(getBaseContext(), main.class);
        startActivity(intent);

2011年6月9日

Layout依比例%分配寬度

類似html中的%width語法,android layout中也有相對應的layout_weight和weightSum.
    <LinearLayout  ...  >
        <TextView ...  android:layout_weight="1"  />
        <TextView ...  android:layout_weight="1"  />
        <TextView ...  android:layout_weight="1"  />
    </LinearLayout>
 
(... 省略)

觸控縮放Touch Zooming WebView

設定Layout和程式權限android.permission.INTERNET
In main.xml
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">
    <WebView
      android:id="@+id/MyIE"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" />
</LinearLayout>
 
In AndroidManifest.xml
....
<uses-permission android:name="android.permission.INTERNET" />
...
呼叫WebView的getSettings()得到WebSettings物件來進行設定。
   WebView ie;
   @Override
   public void onCreate(Bundle savedInstanceState) 
   {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main );
 
       // 取得 WebView物件 
       ie = (WebView) findViewById( R.id.MyIE);        
       // 取得 WebSettings物件 
       WebSettings webSettings = ie.getSettings();  
       // 支援JavaScript
       webSettings.setJavaScriptEnabled(true);             
       // 支援Zoom
       webSettings.setSupportZoom(true);                    
       webSettings.setBuiltInZoomControls(true);
   }

Android 程式的生命週期測試

在每個生命週期事件加上一個Toast訊息,來得知呼叫的順序。
第一次執行程式時,進行起始啟動 OnCreate() > OnStart() > OnResume()

暫停Pause

按下HOME按鈕 OnPause() > OnStop(),程式只是暫停而停止,
當再次啟動程式時 OnReStart() > OnStart() > OnResume()。
  • 當呼叫Intent時,程式會先暫停(OnPause() > OnStop(),然後返回時 OnReStart() > OnStart() > OnResume())。

結束Destroy

按下BACK按鈕或呼叫finish()方法,則會真正的結束程式 OnPause() > OnStop() > onDestroy(),
當再次執行程式時,就如同起始啟動
  • 當旋轉螢幕時,程式會先結束,然後再啟始起動(OnPause() > OnStop() > onDestroy() > OnCreate() > OnStart() > OnResume())。

機器休眠待機時

此時可分為兩種情形
  • 程式在執行狀態:經過多個Pause,Resume,Stop事件後,最後會Destory。當機器醒來時,程式會重新起始啟動OnCreate() > OnStart() > OnResume()。
  • 程式在暫停狀態:當機器醒來重新執行程式時,就如同從暫停模式回來一樣 OnReStart()>OnStart()>OnResume()。
 @Override
  public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       Toast.makeText(this, "onCreate", Toast.LENGTH_LONG).show();
   }
 @Override
 protected void onRestart() {
  // TODO Auto-generated method stub
  super.onRestart();
  Toast.makeText(this, "onRestart", Toast.LENGTH_LONG).show();
 }
 @Override
 protected void onStart() {
  // TODO Auto-generated method stub
  super.onStart();
  Toast.makeText(this, "onStart", Toast.LENGTH_LONG).show();
 }
 @Override
 protected void onResume() {
  // TODO Auto-generated method stub
  super.onResume();
  Toast.makeText(this, "onResume", Toast.LENGTH_LONG).show();
 }
 @Override
 protected void onPause() {
  // TODO Auto-generated method stub
  super.onPause();
  Toast.makeText(this, "onPause", Toast.LENGTH_LONG).show();
 }
 @Override
 protected void onStop() {
  // TODO Auto-generated method stub
  super.onStop();
  Toast.makeText(this, "onStop", Toast.LENGTH_LONG).show();
 }
@Override
 protected void onDestroy() {
  // TODO Auto-generated method stub
  super.onDestroy();
  Toast.makeText(this, "onDestroy", Toast.LENGTH_LONG).show();
 }

全螢幕且不顯示標題列

requestWindowFeature(...)可讓標題欄不顯示,而getWindow().setFlags(...)可以設定全螢幕顯示。

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  WindowManager.LayoutParams.FLAG_FULLSCREEN);        
 
        setContentView(R.layout.main);
        ...
或在AndroidManifest.xml的Activity區塊中插入:
    <application android:icon="@drawable/wusjp" android:label="@string/app_name" >
        <activity   
                  android:theme="@android:style/Theme.NoTitleBar"
                  ...                                                                           
        >
         ...    
        activity>
    application>

2011年6月6日

試用Windows Live Writer

LinearLayout mLinearLayout;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Create a LinearLayout in which to add the ImageView
mLinearLayout = new LinearLayout(this);

// Instantiate an ImageView and define its properties
ImageView i = new ImageView(this);
i.setImageResource(R.drawable.my_image);
i.setAdjustViewBounds(true); // set the ImageView bounds to match the Drawable's dimensions
i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));

// Add the ImageView to the layout and set the layout as the content view
mLinearLayout.addView(i);
setContentView(mLinearLayout);
}

2011年6月2日

Android: MediaPlayer 播放Asset中的檔案

因為Security保密原因media server 無法存取你的程式私有資料目錄,
所以你必須在程式中開檔案,然後使用setDataSource(fd)
將檔案Filedescriptor傳給media player。
當然也可將檔案複製到SD卡,然後播其路徑。

AssetFileDescriptor afd = getAssets().openFd("test.MP3"); 
mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength()); 
mp.prepare();
mp.start();
afd.close();

2011年2月17日

Watson (artificial intelligence software) - Wikipedia, the free encyclopedia

Watson (artificial intelligence software) - Wikipedia, the free encyclopedia

2011年2月16日

The road to 6.77 (Feliks Zemdegs ) - 魔方视频 - 魔方站

The road to 6.77 (Feliks Zemdegs ) - 魔方视频 - 魔方站

2011年2月2日

Solar "Death Ray": Power of 5000 suns!

2011年1月11日

Rubik's Cube: Cross (Advanced)

Rubik's Cube: Cross (Advanced)

2011年1月7日

Amazing Photos by Sarolta Bán | Yurock

Amazing Photos by Sarolta Bán | Yurock

2010年12月27日

7x7x7 Stop Motion Assembly!!

7x7x7 Stop Motion Assembly!

2010年12月21日

M2 / Old Pochmann Blindfold Tutorial - Part 1

M2 / Old Pochmann Blindfold Tutorial - Part 1

2010年12月19日

CFOP速解發明人Jessica Fridrich女士跳過PLL轉出14.33秒

CFOP速解發明人Jessica Fridrich女士跳過PLL轉出14.33秒

2010年12月3日

Dadsense 爸爸之歌 附中文字幕

爸爸之歌

媽媽之歌 The Mom Song 附中文字幕

媽媽之歌

2010年11月24日

XNA 程式設計入門

XNA遊戲迴圈

基本上就是一個迴圈不斷執行,直到使用者結束遊戲。

1.啟始化並載入資源
   1.1 Initialize()
   1.2 LoadGraphicsContent()
2.迴圈開始
   2.1. Update()  //處理遊戲邏輯
   2.2. Draw()     //處理繪圖
3.迴圈結束
4.釋放資源 UnloadGraphicsContent()

2010年11月9日

Direct2D (Windows)

Direct2D (Windows)
踏破鐵鞋無覓處,得來全不費功夫。
Google XNA Draw Rectangle,發現有人寫成一個小Class來做這些事,心中竊喜。
想說那DirectX應該也會有前人的前車之鑑。Google DirectX Rectangle,結果中發現有人回應Direct2D這個東東,真是乖乖隆個咚咚:)

這不就是我想要的嗎?
真是功課作得不如那時勤奮了...那時可真是每天都浸淫在微軟的MSDN Library中呀!

2010年11月2日

SlimDX

SlimDX Homepage
DirectX的簡易介面,裝來試試看。也許是個不錯的選擇?
SlimDX is a free open source framework that enables developers to easily build DirectX applications using C#, VB.NET, and IronPython. It is designed to be an efficient, simple, and lean wrapper that fully encompasses all of Microsoft's gaming and multimedia technologies and exposes them to managed code.

2010/11/01 安裝同時安裝DirectX(2010June)
2010/11/02 寫個小程式吧...