程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java播放聲響類和一個簡略示例

java播放聲響類和一個簡略示例

編輯:關於JAVA

java播放聲響類和一個簡略示例。本站提示廣大學習愛好者:(java播放聲響類和一個簡略示例)文章只能為提供參考,不一定能成為您想要的結果。以下是java播放聲響類和一個簡略示例正文


這裡重要是總結一下若何監聽有未接來電的成績
 
1.1 應用播送吸收器 BrocastReceiver
完成思緒 :
靜態注冊監聽android.intent.action.PHONE_STATE 的播送吸收器 當手機的狀況轉變後將會觸發 onReceive.
手機的狀況分為CALL_STATE_RINGING(響鈴中),CALL_STATE_IDLE(余暇),CALL_STATE_OFFHOOK(忙音).
也就是說當你沒有任何德律風是,狀況是 IDLE ,當接到德律風時是 OFFHOOK ,德律風停止後前往 IDLE 狀況。
記載上一次的手機狀況,假如的手機如今的余暇,前次的狀況響鈴中的話,便可以斷定是未接來電.

<uses-permission android:name="android.permission.READ_PHONE_STATE"/> 
<receiver android:name="com.example.phonestatedemo.receiver.PhoneStateReceiver"> 
   <intent-filter > 
      <action android:name="android.intent.action.PHONE_STATE"/> 
   </intent-filter> 
 </receiver> 

 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.telephony.TelephonyManager; 
import android.util.Log; 
import android.widget.Toast; 
 
public class PhoneStateReceiver extends BroadcastReceiver { 
 
  private static int lastCallState = TelephonyManager.CALL_STATE_IDLE; 
   
  @Override 
  public void onReceive(Context arg0, Intent arg1) { 
    String action = arg1.getAction(); 
    Log.d("PhoneStateReceiver", action ); 
    TelephonyManager telephonyManager = (TelephonyManager) arg0 
        .getSystemService(Context.TELEPHONY_SERVICE); 
    int currentCallState = telephonyManager.getCallState(); 
    Log.d("PhoneStateReceiver", "currentCallState=" + currentCallState ); 
    if (currentCallState == TelephonyManager.CALL_STATE_IDLE) {// 余暇 
      //TODO  
    } else if (currentCallState == TelephonyManager.CALL_STATE_RINGING) {// 響鈴 
      //TODO  
    } else if (currentCallState == TelephonyManager.CALL_STATE_OFFHOOK) {// 接聽 
      //TODO  
    } 
    if(lastCallState == TelephonyManager.CALL_STATE_RINGING &&  
          currentCallState == TelephonyManager.CALL_STATE_IDLE){ 
      Toast.makeText(arg0, "有未接來電", 1).show(); 
    } 
     
    lastCallState = currentCallState; 
 
  } 
 
} 
 

1.2  應用 PhoneStateListener
完成思緒 :
繼續PhoneStateListener後,當手機的狀況轉變後將會觸發onCallStateChanged.手機的狀況分為CALL_STATE_RINGING(響鈴中),CALL_STATE_IDLE(余暇),CALL_STATE_OFFHOOK(忙音).
也就是說當你沒有任何德律風是,狀況是 IDLE ,當接到德律風時是 OFFHOOK ,德律風停止後前往 IDLE 狀況。
記載上一次的手機狀況,假如的手機如今的余暇,前次的狀況響鈴中的話,便可以斷定是未接來電.
 
缺乏:如今的處置不克不及斷定出是用戶能否自動不接德律風.

TelephonyManager telephonyManager = (TelephonyManager) this 
        .getSystemService(Context.TELEPHONY_SERVICE); 
    telephonyManager.listen(new CallStateListener(this), 
        PhoneStateListener.LISTEN_CALL_STATE); 
 

package com.example.phonestatedemo.listener; 
 
import android.content.Context; 
import android.telephony.PhoneStateListener; 
import android.telephony.TelephonyManager; 
import android.util.Log; 
import android.widget.Toast; 
 
public class CallStateListener extends PhoneStateListener { 
  private static int lastetState = TelephonyManager.CALL_STATE_IDLE; // 最初的狀況 
  private Context context; 
 
  public CallStateListener(Context context) { 
    this.context = context; 
 
  } 
 
  @Override 
  public void onCallStateChanged(int state, String incomingNumber) { 
    // TODO Auto-generated method stub 
    super.onCallStateChanged(state, incomingNumber); 
    Log.d("CallStateListener", "onCallStateChanged state=" + state ); 
    // 假如以後狀況為余暇,前次狀況為響鈴中的話,則破觚為以為是未接來電 
    if (lastetState == TelephonyManager.CALL_STATE_RINGING 
        && state == TelephonyManager.CALL_STATE_IDLE) { 
      //TODO 
      Toast.makeText(this.context, "CallStateListener 有未接來電", 1).show(); 
    } 
 
    lastetState = state; 
 
  } 
 
} 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved