2011年1月21日 星期五

Android - 讀取手機電話簿(PhoneBook)

1. 此範例是自動填入文字(AutoCompleteTextView)功能來搜尋電話簿內的聯絡人姓名。

2. 由於我們需取得目前手機的電話簿,因此必需在AndroidManifest.xml內新增一個讀取電話簿的權限。

3. MainActivity.java
package org.me.android_phonebook;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class MainActivity extends Activity {
    private AutoCompleteTextView autoComplete;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        autoComplete = (AutoCompleteTextView) findViewById(R.id.autoComplete);
        //設定自動填入的文字內容
        ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,getContactsName());
        autoComplete.setAdapter(adapter);
    }

    //取得所有聯絡人姓名
    public String[] getContactsName() {
        //取得內容解析器
        ContentResolver contentResolver = this.getContentResolver();
        //設定你要從電話簿取出的欄位
        String[] projection = new String[]{Contacts.People.NAME,Contacts.People.NUMBER};
        //取得所有聯絡人
        Cursor cursor = contentResolver.query(Contacts.People.CONTENT_URI, projection, null, null, Contacts.People.DEFAULT_SORT_ORDER);
        String[] contactsName = new String[cursor.getCount()];
        for (int i = 0; i < cursor.getCount(); i++) {
            //移到指定位置
            cursor.moveToPosition(i);
            //取得第一個欄位
            contactsName[i] = cursor.getString(0);
        }
        return contactsName;
    }
}
4. AndroidManifest.xml
<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="org.me.android_phonebook">
    <application>
         <activity android:name=".MainActivity" android:label="PhoneBook">
            <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.READ_CONTACTS"></uses-permission>
</manifest>
5. 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">" 
    <AutoCompleteTextView
        android:id="@+id/autoComplete"
        android:layout_width="fill_parent"
        android:layout_height="50px">
    </AutoCompleteTextView>
</LinearLayout>
6. 執行之後的畫面。

2011年1月19日 星期三

Android - 通話狀態(PhoneState)

1. 利用繼承PhoneStateListener來實作當通話狀態為閒置、接起或響起時我們所要做的動作。

2. 由於我們需取得目前手機的通話狀態,因此必需在AndroidManifest.xml內新增一個讀取通話狀態的權限。

3. MainActivity.java
package org.me.android_callstate;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        //電話狀態的Listener
        MyPhoneStateListener myPhoneStateListener = new MyPhoneStateListener();
        //取得TelephonyManager
        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        //將電話狀態的Listener加到取得TelephonyManager
        telephonyManager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
    }

    public class MyPhoneStateListener extends PhoneStateListener {
        @Override
        public void onCallStateChanged(int state, String phoneNumber) {
            switch (state) {
                //電話狀態是閒置的
                case TelephonyManager.CALL_STATE_IDLE:
                    break;
                //電話狀態是接起的
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    Toast.makeText(MainActivity.this, "正接起電話…", Toast.LENGTH_LONG).show();
                    break;
                //電話狀態是響起的
                case TelephonyManager.CALL_STATE_RINGING:
                    Toast.makeText(MainActivity.this, phoneNumber + "正打電話來…", Toast.LENGTH_LONG).show();
                    break;
                default:
                    break;
            }
        }
    }
}


4. AndroidManifest.xml
<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="org.me.android_callstate">
    <application>
         <activity android:name=".MainActivity" android:label="MainActivity">
            <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.READ_PHONE_STATE"></uses-permission>
</manifest>


4. 執行之後的畫面。

2011年1月16日 星期日

Android - 畫廊(Gallery)

1. 使用Gallery Widget來達到畫廊的效果。

2. 需建立一個繼承BaseAdapter的物件來放置你要呈現的圖片。

3. MainActivity.java
package org.me.android_gallery;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.Toast;

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        Gallery gallery = (Gallery) findViewById(R.id.gallery);
        ImageAdapter imageAdapter = new ImageAdapter(this);
        //設定圖片來源
        Integer[] mImageIds = {
            R.drawable.android_logo,
            R.drawable.androids,
            R.drawable.cupcake_2009,
            R.drawable.donut_2009,
            R.drawable.eclair_2009
        };
        //設定圖片的位置
        imageAdapter.setmImageIds(mImageIds);
        //圖片高度
        imageAdapter.setHeight(100);
        //圖片寬度
        imageAdapter.setWidth(200);
        gallery.setAdapter(imageAdapter);
        gallery.setOnItemClickListener(new OnItemClickListener(){
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, "您選的是第"+position+"張圖", Toast.LENGTH_LONG).show();
            }
        });
    }
}

4. ImageAdapter.java
package org.me.android_gallery;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    private Integer width;
    private Integer height;
    private Integer[] mImageIds;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);
        //設定圖片來源
        imageView.setImageResource(mImageIds[position]);
        //設定圖片的寬、高
        imageView.setLayoutParams(new Gallery.LayoutParams(width, height));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        return imageView;
    }

    public Integer getHeight() {
        return height;
    }

    public void setHeight(Integer height) {
        this.height = height;
    }

    public Integer[] getmImageIds() {
        return mImageIds;
    }

    public void setmImageIds(Integer[] mImageIds) {
        this.mImageIds = mImageIds;
    }

    public Integer getWidth() {
        return width;
    }

    public void setWidth(Integer width) {
        this.width = width;
    }

    public int getCount() {
        return mImageIds.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }
}


5. 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">
    <Gallery
        android:id="@+id/gallery"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </Gallery>
</LinearLayout>


4. 執行之後的畫面。

2011年1月15日 星期六

Android - 計時器(Timer)

1. 由於若不是Main Thread則無法去變更畫面的Widget內容,需透過android.os.Handler來達到此效果。

2. MainActivity.java
package org.me.android_timer;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

public class MainActivity extends Activity {
    private Long startTime;
    private Handler handler = new Handler();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //取得目前時間
        startTime = System.currentTimeMillis();
        //設定定時要執行的方法
        handler.removeCallbacks(updateTimer);
        //設定Delay的時間
        handler.postDelayed(updateTimer, 1000);
    }

    //固定要執行的方法
    private Runnable updateTimer = new Runnable() {
        public void run() {
            final TextView time = (TextView) findViewById(R.id.timer);
            Long spentTime = System.currentTimeMillis() - startTime;
            //計算目前已過分鐘數
            Long minius = (spentTime/1000)/60;
            //計算目前已過秒數
            Long seconds = (spentTime/1000) % 60;
            time.setText(minius+":"+seconds);
            handler.postDelayed(this, 1000);
        }
    };
}


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"
    >
    <TextView
        android:id="@+id/timer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0:0"
        android:textSize="70sp"
        android:layout_x="90px"
        android:layout_y="160px"
        >
    </TextView>
</AbsoluteLayout>


4. 執行之後的畫面。