ANDROID APP
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
184 บทที่ 7 การติดต่อกับฮาร์ดแวร์ต่างๆ<br />
ในส่วนนี้เราจะใช้ Logcat (จะกล่าวถึงในบทที่ 12 “การตรวจสอบการทำงานของแอพ”) ในการ<br />
แสดงสถานะการทำงานของอุปกรณ์กัน<br />
ชุดคำสั่งที่ 7.9 แสดงแอคทิวิตี้หลักที่จะสร้างอินสแตนซ์ของ PhoneStateListener ซึ่งจะ<br />
เรียกใช้เมธอด onCallStateChanged เพื่อตรวจจับอีเวนต์ของการเปลี่ยนสถานะสายเรียกเข้า<br />
ส่วนเมธอดอื่นๆ ที่สามารถเรียกใช้ได้ก็คือ onCallForwardingIndicator(), onCellLocation-<br />
Changed() และ onDataActivity()<br />
ชุดคำสั่งที่ 7.9 src/com/cookbook/hardware.telephony/HardwareTelephony.java<br />
package com.cookbook.hardware.telephony;<br />
import android.app.Activity;<br />
import android.os.Bundle;<br />
import android.telephony.PhoneStateListener;<br />
import android.telephony.TelephonyManager;<br />
import android.util.Log;<br />
import android.widget.TextView;<br />
public class HardwareTelephony extends Activity {<br />
TextView tv1;<br />
TelephonyManager telManager;<br />
@Override<br />
public void onCreate(Bundle savedInstanceState) {<br />
super.onCreate(savedInstanceState);<br />
setContentView(R.layout.main);<br />
tv1 =(TextView) findViewById(R.id.tv1);<br />
telManager = (TelephonyManager)<br />
getSystemService(TELEPHONY_SERVICE);<br />
}<br />
telManager.listen(new TelListener(),<br />
PhoneStateListener.LISTEN_CALL_STATE);<br />
private class TelListener extends PhoneStateListener {<br />
public void onCallStateChanged(int state, String incomingNumber) {<br />
super.onCallStateChanged(state, incomingNumber);