您的位置:首页 > 移动开发 > Android开发

Android学习篇章32-Broadcast广播基础-通话监听

2013-11-02 19:55 453 查看
Mainactivity:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

TelephonyManager tm=null;

@Override
protected void onResume() {
if(tm==null)
{
tm=(TelephonyManager)this.getSystemService(Service.TELEPHONY_SERVICE);
//注册监听器
tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
super.onResume();
}
@Override
protected void onPause() {
//取消监听
//		if(tm!=null)
//			tm.listen(null, PhoneStateListener.LISTEN_CALL_STATE);
super.onPause();
}

PhoneStateListener  listener=new PhoneStateListener()
{
//手机通话状态发生改变时会执行
@Override
public void onCallStateChanged(int state, String incomingNumber) {
// TODO Auto-generated method stub
switch(state)
{
case   TelephonyManager.CALL_STATE_IDLE:
//空闲状态
Log.i("test", "-----电话处于空闲状态 电话:"+incomingNumber);
break;
case  TelephonyManager.CALL_STATE_RINGING:
//响铃状态
Log.i("test", "----电话响铃了 电话:"+incomingNumber);
break;
case  TelephonyManager.CALL_STATE_OFFHOOK:
//摘机   也就是通话中
Log.i("test", "-----电话通话中 电话:"+incomingNumber);
break;

}
//super.onCallStateChanged(state, incomingNumber);
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}


MyCallingBroadcastReceiver:

public class MyCallingBroadcastReceiver  extends BroadcastReceiver {

//监听打出去电话的广播
public  static  final String  NEW_OUT_CALL="android.intent.action.NEW_OUTGOING_CALL";
//通话状态的改变
public  static  final String PHONE_STATE="android.intent.action.PHONE_STATE";
@Override
public void onReceive(Context context, Intent intent) {
String action=intent.getAction();
if(NEW_OUT_CALL.equals(action))
{
//获得对方号码
String to=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i("test", "正在打出电话,对方号码"+to);
Toast.makeText(context, "正在打出电话,对方号码"+to, Toast.LENGTH_LONG).show();
}else if(PHONE_STATE.equals(action))
{

//拨打电话  也就是当前手机处于主叫方  将不会有响铃状态
//电话处于被叫方  三种状态都会有
//获得通话服务
//获得被叫时  拨入进来的手机号码
Bundle  data=intent.getExtras();
Set<String>  keys=data.keySet();
for(String key : keys)
{
Log.i("test", "key="+key);
}
//只有在被叫时 响铃状态才有
String phone=intent.getStringExtra("incoming_number");
TelephonyManager tm=(TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);
int phonestate=tm.getCallState();
//int phonestate=intent.getIntExtra("state", 0);
switch(phonestate)
{
case   TelephonyManager.CALL_STATE_IDLE:
//空闲状态
Log.i("test", "电话处于空闲状态 电话:"+phone);
break;
case  TelephonyManager.CALL_STATE_RINGING:
//响铃状态
Log.i("test", "电话响铃了 电话:"+phone);
break;
case  TelephonyManager.CALL_STATE_OFFHOOK:
//摘机   也就是通话中
Log.i("test", "电话通话中 电话:"+phone);
break;
}
}
}
}


权限与注册:

<!-- 获得读取通话状态的许可权限 -->
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<!-- 获得处理打出电话的许可权限 -->
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.calling.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.calling.MyCallingBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
</application>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: