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. 執行畫面


2010年7月12日 星期一

Android學習筆記 - 撥打電話(PhoneCall)

1. 要能夠透過程式撥打電話,需要先取得CALL_PHONE的權限,因此需在AndroidManifest.xml允許存取android.permission.CALL_PHONE這個權限。

2.PhoneCall.java
package tw.nicky.PhoneCall;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class PhoneCall extends Activity {
 private Button callButton;
 private EditText editText;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        callButton = (Button)findViewById(R.id.callButton);
        editText = (EditText)findViewById(R.id.editText);
        
        //按下call按鈕
        callButton.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    String phoneNumber = editText.getText().toString();
    //啟動撥打電話程式
    Intent intentDial = new Intent("android.intent.action.CALL",Uri.parse("tel:"+phoneNumber));
    startActivity(intentDial);
   }
  });
    }
}

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">
<EditText 
android:id="@+id/editText" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content">
</EditText>
<Button 
android:id="@+id/callButton" 
android:text="Call" 
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.PhoneCall"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".PhoneCall"
                  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.CALL_PHONE"/>
</manifest> 


5. 執行畫面


2010年7月11日 星期日

Android學習筆記 - 接收SMS訊息(SMSReceiver)

1. 需繼承BroadcastReceiver並覆寫onReceive方法,將所有接收到SMS訊息要處理的動作寫在這個方法內。要接收SMS訊息需要使用者先同意接收SMS訊息的權限,因此需在AndroidManifest.xml允許存取android.permission.RECEIVE_SMS這個權限。

2.SMSReceiver.java
package tw.nicky.ReceiveSMSExample;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SMSReceiver extends BroadcastReceiver{
 @Override
 public void onReceive(Context context, Intent intent) {
  //判斷這個是接收到SMS的Broadcast
  if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
   Bundle bundle = intent.getExtras();
   Object[] pdus = (Object[]) bundle.get("pdus");
   SmsMessage[] smsMessages = new SmsMessage[pdus.length];
   //將pdu轉成SmsMessage
   for(int i=0;i
3. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="tw.nicky.ReceiveSMSExample" android:versionCode="1"
 android:versionName="1.0">
 <application android:icon="@drawable/icon" android:label="ReceiveSMSExample">
  <receiver android:name=".SMSReceiver">
   <intent-filter>
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
   </intent-filter>
  </receiver>
 </application>
 <uses-permission android:name="android.permission.RECEIVE_SMS" />
</manifest> 
5. 執行畫面


2010年7月7日 星期三

Android學習筆記 - 錄音(MediaRecorder)

1. 要進行錄音需先取得錄音的權限,加上需將錄音檔寫到記憶卡,因此需在AndroidManifest.xml允許存取android.permission.RECORD_AUDIO及android.permission.WRITE_EXTERNAL_STORAGE這二個權限。

2.MediaRecorderExample.java
package tw.nicky;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;

public class MediaRecorderExample extends Activity {
 private Button recordButn;
 private Button stopButn;
 private MediaRecorder mediaRecorder = null;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  recordButn = (Button) findViewById(R.id.recordButn);
  stopButn = (Button) findViewById(R.id.stopButn);

  //錄音
  recordButn.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
    //設定錄音檔名
    String fileName = "record.amr";
    try {
     File SDCardpath = Environment.getExternalStorageDirectory();
                    File myDataPath = new File( SDCardpath.getAbsolutePath() + "/download" );
                    if( !myDataPath.exists() ) myDataPath.mkdirs();
     File recodeFile = new File(SDCardpath.getAbsolutePath() + "/download/"+fileName);
     
     mediaRecorder = new MediaRecorder();

     //設定音源
     mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
     //設定輸出檔案的格式
     mediaRecorder
       .setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
     //設定編碼格式
     mediaRecorder
       .setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
     //設定錄音檔位置
     mediaRecorder
       .setOutputFile(recodeFile.getAbsolutePath());

     mediaRecorder.prepare();

     //開始錄音
     mediaRecorder.start();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  });

  //停止錄音
  stopButn.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
    if(mediaRecorder != null) {
     mediaRecorder.stop();
     mediaRecorder.release();
     mediaRecorder = null;
    }
   }
  });

 }
}
3. main.xml(Layout)
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<Button
android:id="@+id/recordButn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="錄音"
android:layout_x="68px"
android:layout_y="44px"
>
</Button>
<Button
android:id="@+id/stopButn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止"
android:layout_x="170px"
android:layout_y="43px"
>
</Button>
</AbsoluteLayout>

4. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="tw.nicky"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MediaRecorderExample"
                  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.WRITE_EXTERNAL_STORAGE"></uses-permission>
    <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
</manifest> 


5. 執行畫面


Android學習筆記 - 音量調整(AudioManager)

1. 音量調整主要是透過AudioManager來對手機音量進行調整,可以調整的模式有:正常模式、靜音模式、震動模式、音量增大、音量減少。

2.AudioManagerExample .java
package tw.nicky;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class AudioManagerExample extends Activity {
    private Button normalButn;
    private Button vibrateButn;
    private Button silentButn;
    private Button upButn;
    private Button downButn;
    private AudioManager audioManager;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        normalButn = (Button)findViewById(R.id.normalButn);
        vibrateButn = (Button)findViewById(R.id.vibrateButn);
        silentButn = (Button)findViewById(R.id.silentButn);
        upButn = (Button)findViewById(R.id.upButn);
        downButn = (Button)findViewById(R.id.downButn);
        
        //取得音量控制器
        audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        
        //正常模式
        normalButn.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
   }
  });
        
        //震動模式
        vibrateButn.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
   }
  });
        
        //靜音模式
        silentButn.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
   }
  });
        
        //增大音量
        upButn.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    audioManager.adjustVolume(AudioManager.ADJUST_RAISE, 0);
   }
  });
        
        //減少音量
        downButn.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    audioManager.adjustVolume(AudioManager.ADJUST_LOWER, 0);
   }
  });
    }
}

3. main.xml(Layout)
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<Button
android:id="@+id/vibrateButn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="震動"
android:layout_x="106px"
android:layout_y="16px"
>
</Button>
<Button
android:id="@+id/silentButn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="靜音"
android:layout_x="189px"
android:layout_y="14px"
>
</Button>
<Button
android:id="@+id/normalButn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="正常"
android:layout_x="28px"
android:layout_y="17px"
>
</Button>
<Button
android:id="@+id/upButn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="大聲"
android:layout_x="66px"
android:layout_y="97px"
>
</Button>
<Button
android:id="@+id/downButn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小聲"
android:layout_x="144px"
android:layout_y="97px"
>
</Button>
</AbsoluteLayout>



4. 執行畫面


2010年7月6日 星期二

Android學習筆記 - 存取SD卡

1. 由於寫入資料到SD卡是有需要額外的權限,因此需在AndroidManifest.xml允許存取android.permission.WRITE_EXTERNAL_STORAGE這個權限。

2.AccessSDCardExample.java
package tw.nicky;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;

public class AccessSDCardExample extends Activity {

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //判斷SD卡是否存在
        if(!Environment.getExternalStorageState().equals(Environment.MEDIA_REMOVED) ){
            try {
             //取得SD卡路徑
                File SDCardpath = Environment.getExternalStorageDirectory();
                File myDataPath = new File( SDCardpath.getAbsolutePath() + "/myData" );
                if( !myDataPath.exists() ) myDataPath.mkdirs();
                //將資料寫入到SD卡
                FileWriter myFile = new FileWriter( SDCardpath.getAbsolutePath() + "/myData/test.txt" );
                myFile.write("This is a test.");
                myFile.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
         }
    }
}

3. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="tw.nicky"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AccessSDCardExample"
                  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.WRITE_EXTERNAL_STORAGE" />
</manifest> 


4. SD卡內容