zoukankan      html  css  js  c++  java
  • Android广播接实现电话的监听(电话的状态,拦截)

    原文地址:http://www.cnblogs.com/zyw-205520/archive/2013/01/25/2876844.html

    Android广播接实现电话的监听  

    1:需要在AndroidManifest.xml清单中添加权限 

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

    2:注册广播 要注意事件的级别 (android中的级别-1000~1000)

    <receiver android:name=".PhoneBroadcastReceiver">
    <intent-filter android:priority="1000">
    <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
    <action android:name="android.intent.action.PHONE_STATE"/>
    </intent-filter>
    </receiver>

    详细配置请看 AndroidManifest.xml

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.zyw.broadcastsendsms"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="15" />
    
        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".SendSMS"
                android:label="@string/title_activity_send_sms" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
                
            </activity>
            
            <receiver android:name=".PhoneBroadcastReceiver">
                <intent-filter android:priority="1000">
                    <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
                    <action android:name="android.intent.action.PHONE_STATE"/>   
                </intent-filter>
            </receiver>
            
        </application>
    
        <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
        <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    </manifest>

    3:编写广播的实现类 这里需要继承BroadcastReceiver 实现onReceive()方法

    程序的源代码如下:

    package com.zyw.broadcastsendsms;
    
    import android.app.Service;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.telephony.TelephonyManager;
    import android.util.Log;
    
    public class PhoneBroadcastReceiver extends BroadcastReceiver {
    
        private static final String TAG = "message";
        private static boolean mIncomingFlag = false;
        private static String mIncomingNumber = null;
    
        @Override
        public void onReceive(Context context, Intent intent) {
            // 如果是拨打电话
            if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
                mIncomingFlag = false;
                String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
                Log.i(TAG, "call OUT:" + phoneNumber);
                
            } else {
                // 如果是来电
                TelephonyManager tManager = (TelephonyManager) context
                        .getSystemService(Service.TELEPHONY_SERVICE);
                switch (tManager.getCallState()) {
                
                case TelephonyManager.CALL_STATE_RINGING:
                    mIncomingNumber = intent.getStringExtra("incoming_number");
                    Log.i(TAG, "RINGING :" + mIncomingNumber);
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    if (mIncomingFlag) {
                        Log.i(TAG, "incoming ACCEPT :" + mIncomingNumber);
                    }
                    break;
                case TelephonyManager.CALL_STATE_IDLE:
                    if (mIncomingFlag) {
                        Log.i(TAG, "incoming IDLE");
                    }
                    break;
                }
            }
        }
    
        
        /*@Override
        public void onReceive(Context context, Intent intent) {
            String number = getResultData();
            if("5556".equals(number)){
                setResultData(null);//挂断
            }else{
                number = "12593"+ number; //其他,则加区号
                setResultData(number);
            }
        }*/
    }
    文章转载于网络,如有侵权,请原创留言;内容如有不妥,请各位园友提宝贵意见或建议。所有文章均处于编辑状态。。。。。。百度贴吧:流水小桥吧 如有问题,请点击页面左上角“给我写信”发邮件留言!
  • 相关阅读:
    URAL1204. Idempotents(扩展欧几里得)
    URAL1049. Brave Balloonists
    URAL1133. Fibonacci Sequence(二分)
    URAL1352. Mersenne Primes
    URAL1118. Nontrivial Numbers
    hdu3270Arranging Your Team(dfs)
    Codeforces Round #209 (Div. 2)C
    KMP
    树状数组求逆序对
    poj2352
  • 原文地址:https://www.cnblogs.com/flyoung/p/4929807.html
Copyright © 2011-2022 走看看