2011年12月16日 星期五

PhoneStateListener_手機通話狀態

手機通話狀態,必須繼承PhoneStateListener這個類別
以下為API的reference 

Class Overview


A listener class for monitoring changes in specific telephony states on the device, including service state, signal strength, message waiting indicator (voicemail), and others.


Override the methods for the state that you wish to receive updates for, and pass your PhoneStateListener object, along with bitwise-or of the LISTEN_ flags to TelephonyManager.listen().


Note that access to some telephony information is permission-protected. Your application won't receive updates for protected information unless it has the appropriate permissions declared in its manifest file. Where permissions apply, they are noted in the appropriate LISTEN_ flags.


在監測設備上的特定電話的國家,包括服務的狀態,信號強度,消息等待指示(語音信箱),及其他變化的一個監聽器類別。


如果是在覆蓋的狀態,您希望收到更新的方法,需透過 PhoneStateListener,或LISTEN_ flags TelephonyManager.listen()。


需要注意的是一些電話信息的接聽權限保護。您的應用程序將不會收到保護信息的更新,除非它已在其清單文件中聲明的適當的權限。


權限申請需要在適當的LISTEN_ flags。


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


<uses-permission android:name="android.permission.READ_PHONE_STATE"/>


package com.demo.android.PhoneStateListener;

import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.TextView;
import android.widget.Toast;

public class PhoneState extends PhoneStateListener {
private TextView textView;
private Context context;

public PhoneState(Context c){
super();
context = c;
}

@Override
public void onCallStateChanged(int state, String incomingNumber){
super.onCallStateChanged(state, incomingNumber);

switch(state){
case TelephonyManager.CALL_STATE_IDLE:
//待機
textView.setText("待機");
break;

case TelephonyManager.CALL_STATE_OFFHOOK:
//通話中
textView.setText("通話中");
break;

case TelephonyManager.CALL_STATE_RINGING:
//來電
textView.setText(incomingNumber+"來電");

break;
}
Toast.makeText(context, textView.getText().toString(), Toast.LENGTH_SHORT).show();

}

public void setTextView(TextView tv){
textView = tv;
textView.setText("待機");
}

}

========================================================================

package com.demo.android.PhoneStateListener;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.TextView;

public class PhoneStateListenerActivity extends Activity {
private PhoneState phoneState;
private TextView phoneStateTv;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        phoneStateTv = (TextView)findViewById(R.id.phoneStateTv);
        phoneStateTv.setText("123");
        
        phoneState = new PhoneState(this);
        phoneState.setTextView(phoneStateTv);
        
        TelephonyManager telMgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        telMgr.listen(phoneState, PhoneState.LISTEN_CALL_STATE);
    }
}




沒有留言:

張貼留言