发布于 2016-03-05 22:47:14 | 196 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要介绍了Android中监听未接来电的2种方法,本文讲解了使用广播接收器 BrocastReceiver和使用 PhoneStateListener二种方法,需要的朋友可以参考下

这里主要是总结一下如何监听有未接来电的问题
 
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; 
 
  } 
 
} 



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务