1、接Android 电话秀(一)

2、捕获电话的状态:

package com.ray.test;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.telephony.TelephonyManager;import android.util.Log;/** *  * [捕获电话,包括开机自动动]<BR> * [功能详细描述] *  * @author Administrator * @version [ 2011-8-11] */public class CallListener extends BroadcastReceiver{    private static final String TAG = "ray";    private static String callInNumber;    private static String callOutNumber;    private static boolean outGoingflag = false;    @Override    public void onReceive(Context context, Intent arg1)    {        Log.e(TAG, arg1.getAction().toString());        // 拨号        if (arg1.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL))        {            Log.e(TAG, "2222222222222");            //            incomingFlag = false;            callOutNumber = arg1.getStringExtra(Intent.EXTRA_PHONE_NUMBER);            Log.i(TAG, "call OUT:" + callOutNumber);            String name = arg1.getStringExtra("name");            Log.i(TAG, "name" + name);            outGoingflag = true;            return;        }        TelephonyManager teleMgr = (TelephonyManager) context            .getSystemService(Context.TELEPHONY_SERVICE);        //响铃阶段        if (teleMgr.getCallState() == TelephonyManager.CALL_STATE_RINGING)        {            try            {                Thread.sleep(2000);            }            catch (InterruptedException e)            {                // TODO Auto-generated catch block                e.printStackTrace();            }            Log.e(TAG, "CALL_STATE_RINGING");            Bundle bundle = arg1.getExtras();            callInNumber = bundle.getString("incoming_number");            //这里可以加入根据incomingNumber判断是主叫还是被叫,根据当前主被叫选择启动不同的Activity。            Intent intent = new Intent();            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);            intent.setClass(context, TestCallActivity.class);            intent.putExtra("callInNumber", callInNumber);            context.startActivity(intent);            return;        }        //        //摘机状态:播放显示通话过程动画        if (teleMgr.getCallState() == TelephonyManager.CALL_STATE_OFFHOOK)        {            if (outGoingflag)            {                Log.e(TAG, "phoneNumber " + callOutNumber);                Intent intent = new Intent();                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                intent.setClass(context, TestCallActivity.class);                intent.putExtra("callOutNumber", callOutNumber);                Log.i(TAG, "66666666");                context.startActivity(intent);            }            return;        }        //        //        //待机状态:回到电话振铃前的初始用户界面,        if (teleMgr.getCallState() == TelephonyManager.CALL_STATE_IDLE)        {            Log.e(TAG, "TelephonyManager.CALL_STATE_IDLE");            return;        }    }}


3、展示界面:

package com.ray.test;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.os.RemoteException;import android.telephony.PhoneStateListener;import android.telephony.TelephonyManager;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;import com.android.internal.telephony.ITelephony;/*** *  * [电话秀界面]<BR> * [功能详细描述] *  * @author Administrator * @version [ 2011-8-11] */public class TestCallActivity extends Activity implements OnClickListener{    private String callInNumber = "";    private String callOutNumber = "";    private TextView phoneName;    private TextView phoneNumber;    private Button callAnswer;    private Button callEnd;    private Button callcancel;    private ITelephony iTelephony;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.telshow);        try        {            Thread.sleep(1000);        }        catch (InterruptedException e)        {            // TODO Auto-generated catch block            e.printStackTrace();        }        //        number = getIntent().getExtras().getString("number");        initView();        //        phoneNumber.setText(number);        //         打电话进入        callInNumber = getIntent().getExtras().getString("callInNumber");        Log.e("ray", "callInNumber" + callInNumber);        if (callInNumber != null)        {            phoneNumber.setText(callInNumber);            callAnswer.setVisibility(View.VISIBLE);            callcancel.setVisibility(View.VISIBLE);            callEnd.setVisibility(View.GONE);        }        callOutNumber = getIntent().getExtras().getString("callOutNumber");        Log.e("ray", "callOutNumber" + callOutNumber);        if (callOutNumber != null)        {            phoneNumber.setText(callOutNumber);            callAnswer.setVisibility(View.GONE);            callcancel.setVisibility(View.GONE);            callEnd.setVisibility(View.VISIBLE);            callEnd.setText("结束通话");        }        initialItelephony();    }    private void initView()    {        phoneName = (TextView) findViewById(R.id.phone_name);        phoneNumber = (TextView) findViewById(R.id.phone_number);        callAnswer = (Button) findViewById(R.id.call_answer);        callEnd = (Button) findViewById(R.id.call_end);        callcancel = (Button) findViewById(R.id.call_cancel);        callAnswer.setOnClickListener(this);        callcancel.setOnClickListener(this);        callEnd.setOnClickListener(this);    }    @Override    public void onClick(View v)    {        // TODO Auto-generated method stub        switch (v.getId())        {            case R.id.call_end:                try                {                    iTelephony.endCall();                }                catch (RemoteException e)                {                    // TODO Auto-generated catch block                    e.printStackTrace();                }                break;            case R.id.call_cancel:                try                {                    iTelephony.endCall();                }                catch (RemoteException e)                {                    // TODO Auto-generated catch block                    e.printStackTrace();                }                break;            case R.id.call_answer:                try                {                    iTelephony.answerRingingCall();                }                catch (RemoteException e)                {                    // TODO Auto-generated catch block                    e.printStackTrace();                }                break;            default:                break;        }    }    public class MyPhoneCallListener extends PhoneStateListener    {        @Override        public void onCallStateChanged(int state, String incomingNumber)        {            // TODO Auto-generated method stub            //                     switch (state)            {                // 挂断                case TelephonyManager.CALL_STATE_IDLE:                    Log.e("ray", "CALL_STATE_IDLE");                    callAnswer.setVisibility(View.GONE);                    callcancel.setVisibility(View.GONE);                    callEnd.setVisibility(View.VISIBLE);                    callEnd.setText("通话已结束");                    try                    {                        Thread.sleep(2000);                    }                    catch (InterruptedException e)                    {                        // TODO Auto-generated catch block                        e.printStackTrace();                    }                    finish();                    break;                //响铃中                case TelephonyManager.CALL_STATE_RINGING:                    Log.e("ray", "CALL_STATE_RINGING");                    callAnswer.setVisibility(View.VISIBLE);                    callcancel.setVisibility(View.VISIBLE);                    callEnd.setVisibility(View.GONE);                    break;                //通话中                case TelephonyManager.CALL_STATE_OFFHOOK:                    callAnswer.setVisibility(View.GONE);                    callcancel.setVisibility(View.GONE);                    callEnd.setVisibility(View.VISIBLE);                    callEnd.setText("结束通话");                    Log.e("ray", "CALL_STATE_OFFHOOK");                    break;                default:                    break;            }        }    }    public void initialItelephony()    {        TelephonyManager teleMgr = (TelephonyManager) this            .getSystemService(Context.TELEPHONY_SERVICE);        MyPhoneCallListener myPhoneCallListener = new MyPhoneCallListener();        teleMgr.listen(myPhoneCallListener,            PhoneStateListener.LISTEN_CALL_STATE);        Class<TelephonyManager> c = TelephonyManager.class;        Method getITelephonyMethod = null;        try        {            getITelephonyMethod = c.getDeclaredMethod("getITelephony",                (Class[]) null);            getITelephonyMethod.setAccessible(true);        }        catch (SecurityException e)        {            // TODO Auto-generated catch block            e.printStackTrace();        }        catch (NoSuchMethodException e)        {            // TODO Auto-generated catch block            e.printStackTrace();        }        try        {            iTelephony = (ITelephony) getITelephonyMethod.invoke(teleMgr,                (Object[]) null);        }        catch (IllegalArgumentException e)        {            // TODO Auto-generated catch block            e.printStackTrace();        }        catch (IllegalAccessException e)        {            // TODO Auto-generated catch block            e.printStackTrace();        }        catch (InvocationTargetException e)        {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}


3.添加权限,及接收:

<receiver android:name="CallListener"><intent-filter><action android:name="android.intent.action.PHONE_STATE"></action><category android:name="android.intent.category.DEFAULT"></category></intent-filter><intent-filter><action android:name="android.intent.action.NEW_OUTGOING_CALL"></action><category android:name="android.intent.category.DEFAULT"></category></intent-filter><intent-filter><action android:name="android.intent.action.BOOT_COMPLETED"></action><category android:name="android.intent.category.DEFAULT"></category></intent-filter></receiver></application><!-- 新增撥出電話的權限 --><uses-permission android:name="android.permission.CALL_PHONE"></uses-permission><uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission><uses-permission android:name="android.permission.MODIFY_PHONE_STATE"></uses-permission><uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission>


4、页面XML:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent" android:layout_height="fill_parent"><LinearLayout android:id="@+id/linearLayout1"android:layout_alignParentTop="true" android:layout_height="wrap_content"android:layout_width="fill_parent" android:orientation="vertical"><TextView android:text="电话号码" android:id="@+id/phone_number"android:layout_width="wrap_content" android:layout_height="wrap_content"android:layout_gravity="center"></TextView><TextView android:text="呢称" android:id="@+id/phone_name"android:layout_width="wrap_content" android:layout_height="wrap_content"android:layout_gravity="center"></TextView></LinearLayout><LinearLayout android:layout_width="fill_parent"android:layout_height="wrap_content" android:orientation="horizontal"android:id="@+id/call_bottom_layout" android:layout_alignParentBottom="true"><Button android:layout_width="0dip" android:layout_weight="1"android:id="@+id/call_answer " android:layout_height="wrap_content"android:text="接听" android:visibility="gone"></Button><Button android:text="结束通话" android:id="@+id/call_end"android:layout_width="0dip" android:layout_height="wrap_content"android:layout_weight="1" android:visibility="gone"></Button><Button android:layout_width="0dip" android:layout_weight="1"android:id="@+id/call_cancel" android:layout_height="wrap_content"android:text="拒绝" android:visibility="gone"></Button></LinearLayout></RelativeLayout>


更多相关文章

  1. android打电话,接电话,挂电话过程
  2. 我的Android进阶之旅------>Android电话实例
  3. 博文视点大讲堂35期-It's Android Time:程序员创富有道! 圆满结束
  4. android模拟打电话的应用小程序
  5. 在Android中根据联系人查询电话号码
  6. android 呼出电话的监听(去电监听)
  7. android 呼入电话的监听(来电监听)

随机推荐

  1. android访问webservice
  2. Android(安卓)恐怖幽灵音效 程序(源码详解
  3. Android剪切板
  4. MeidaProvider 流程学习笔记
  5. Android(安卓)AsyncTask 异步任务取消
  6. Android动态刷新listview中的数据
  7. Not targeting the latest versions of A
  8. 44、头像上传
  9. Android实现一个选择器-recycleview滚动
  10. 【Android】异步加载图片-------不错的思