Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 實現電話攔截及攔截提示音功能的開發

Android 實現電話攔截及攔截提示音功能的開發

編輯:關於Android編程

  本文所講的內容是在Android系統中如何寫程序進行電話攔截,並發出攔截提示音提醒用戶,可以說此功能還是比較實用的。

       1、電話攔截

       這個功能大家可能都知道了,就是利用反射原理調用ITelephony的隱藏方法來實現。

       2、攔截後提示忙音/空號/已關機/已停機

       這個功能其實是要用到MMI指令,具體如何設置呼叫轉移的指定可以參考這裡 http://baike.baidu.com/view/206402.html?fromTaglist。

       在本文中我們會用到“遇忙轉移”的功能。中國移動的設置方式是 **67#電話號碼#,取消方式為 ##67#。”無條件轉移“用21代替67即可。這兩個指令可以直接在手機的撥號界面輸入並撥號測試。ITelephony的endcall方法掛斷電話後,會提示電話忙。如果事前設置好了忙時轉移到一個空號/已關機/已停機的電話號碼,就會提示您撥的電話號碼是空號/已關機/已停機。

       其實大家可以下載 xxx衛士看下,它設置來電拒接模式後,都是會啟動設置MMI指令的界面。然後再去“設置->通話設置->來電轉接”,看看 “占線時轉接” 設置好的電話號碼,就可以知道空號/已關機/已停機對應的電話號碼是什麼了。

       1、修改一下BLOCKED_NUMBER這個變量值,把它設置為你要測試攔截的電話號碼。

       2、全部功能是在一個Activity裡實現的,所以大家要先運行這個Activity,然後點擊“設置呼叫轉移”,設置好呼叫轉移後,不要關閉這個Activity,關了就攔截不了電話了。有心的朋友可以自己去寫一個Service在後台運行攔截功能。

       實現方式1:

       代碼如下:

package net.toeach.android.callforwarding;   
  
import java.lang.reflect.Method;   
  
import android.app.Activity;   
import android.content.BroadcastReceiver;   
import android.content.Context;   
import android.content.Intent;   
import android.content.IntentFilter;   
import android.media.AudioManager;   
import android.net.Uri;   
import android.os.Bundle;   
import android.os.Handler;   
import android.os.Message;   
import android.os.RemoteException;   
import android.telephony.TelephonyManager;   
import android.util.Log;   
import android.view.View;   
import android.view.View.OnClickListener;   
  
import com.android.internal.telephony.ITelephony;   
  
/**  
 * 演示如何設置呼叫轉移,攔截電話(攔截後提示為空號)的例子  
 * @author Tony from ToEach.  
 * @email [email protected]  
 */  
public class MainActivity extends Activity {   
 private static final String TAG = MainActivity.class.getSimpleName();   
    
 private final static int OP_REGISTER = 100;   
 private final static int OP_CANCEL = 200;   
   
 private final static String BLOCKED_NUMBER = "1892501xxxx";//要攔截的號碼   
 //占線時轉移,這裡13800000000是空號,所以會提示所撥的號碼為空號   
  private final String ENABLE_SERVICE = "tel:**67*13800000000%23";   
  //占線時轉移   
  private final String DISABLE_SERVICE = "tel:%23%2367%23";   
  
 private IncomingCallReceiver mReceiver;   
  private ITelephony iTelephony;   
  private AudioManager mAudioManager;   
   
  @Override  
  public void onCreate(Bundle savedInstanceState) {   
    super.onCreate(savedInstanceState);   
    setContentView(R.layout.main);   
      
    findViewById(R.id.btnEnable).setOnClickListener(new OnClickListener(){   
  public void onClick(View v) {   
     //設置呼叫轉移   
     Message message = mHandler.obtainMessage();   
  message.what = OP_REGISTER;   
  mHandler.dispatchMessage(message);   
  }   
    });   
      
    findViewById(R.id.btnDisable).setOnClickListener(new OnClickListener(){   
  public void onClick(View v) {   
  //取消呼叫轉移   
       Message message = mHandler.obtainMessage();   
    message.what = OP_CANCEL;   
    mHandler.dispatchMessage(message);   
  }   
    });   
      
    mReceiver = new IncomingCallReceiver();   
 IntentFilter filter = new IntentFilter("android.intent.action.PHONE_STATE");     
    registerReceiver(mReceiver, filter);// 注冊BroadcastReceiver   
      
    mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);   
      
    //利用反射獲取隱藏的endcall方法   
    TelephonyManager telephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);   
 try {   
  Method getITelephonyMethod = TelephonyManager.class.getDeclaredMethod("getITelephony", (Class[]) null);   
  getITelephonyMethod.setAccessible(true);   
  iTelephony = (ITelephony) getITelephonyMethod.invoke(telephonyMgr, (Object[]) null);   
   } catch (Exception e) {   
   e.printStackTrace();   
   }   
  }   
    
  private Handler mHandler = new Handler() {   
 public void handleMessage(Message response) {   
   int what = response.what;   
   switch(what) {   
    case OP_REGISTER:{   
    Intent i = new Intent(Intent.ACTION_CALL);   
       i.setData(Uri.parse(ENABLE_SERVICE));   
       startActivity(i);   
    break;   
    }   
    case OP_CANCEL:{   
    Intent i = new Intent(Intent.ACTION_CALL);   
       i.setData(Uri.parse(DISABLE_SERVICE));   
       startActivity(i);   
    break;   
    }   
   }   
 }   
 };   
   
 private class IncomingCallReceiver extends BroadcastReceiver{   
 @Override  
 public void onReceive(Context context, Intent intent) {   
  String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);   
     Log.i(TAG, "State: "+ state);   
       
  String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);   
     Log.d(TAG, "Incomng Number: " + number);   
       
     if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)){//電話正在響鈴        
     if(number.equals(BLOCKED_NUMBER)){//攔截指定的電話號碼   
      //先靜音處理   
      mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);   
      Log.d(TAG, "Turn ringtone silent");   
        
      try {   
      //掛斷電話   
   iTelephony.endCall();   
   } catch (RemoteException e) {   
   e.printStackTrace();   
   }   
     
   //再恢復正常鈴聲   
         mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);   
     }   
     }   
 }   
 }   
}  

         AndroidManifest.xml 如下:

<?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android "  
   package="net.toeach.android.callforwarding"  
   android:versionCode="1"  
   android:versionName="1.0">  
  <application android:icon="@drawable/icon" android:label="@string/app_name">  
    <activity android:name=".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>  
  
  </application>  
  <uses-sdk android:minSdkVersion="8" />  
    
  <uses-permission android:name="android.permission.READ_PHONE_STATE"/>  
  <uses-permission android:name="android.permission.CALL_PHONE"/>  
  
</manifest>   

        實現方式2:

       1、建立包android.refuseCalling。

       refuseCalling.java代碼如下:

package android.refuseCalling;   
  
import android.app.Activity;   
import android.net.Uri;   
import android.os.Bundle;   
  
import java.lang.reflect.InvocationTargetException;   
import java.lang.reflect.Method;   
  
import android.content.Context;   
import android.content.Intent;   
import android.os.RemoteException;   
import android.telephony.PhoneStateListener;   
import android.telephony.TelephonyManager;   
import android.util.Log;   
import android.widget.TextView;   
import com.android.internal.telephony.ITelephony;   
  
public class refuseCalling extends Activity {   
  
  private static final String TAG = "Telephony";   
  private TextView view = null;   
  private TelephonyManager tManager = null;   
  private ITelephony iTelephony = null;   
     
   //占線時轉移,提示所撥的號碼為空號   
  private final String ENABLE_SERVICE = "tel:**67*13800000000%23";   
   //占線時轉移,提示所撥的號碼為關機   
  private final String ENABLE_POWEROFF_SERVICE = "tel:**67*13810538911%23";   
  //占線時轉移,提示所撥的號碼為停機   
  private final String ENABLE_STOP_SERVICE = "tel:**21*13701110216%23";   
     
  //占線時轉移   
  private final String DISABLE_SERVICE = "tel:%23%2321%23";   
  
  @Override  
  protected void onCreate(Bundle savedInstanceState) {   
    super.onCreate(savedInstanceState);   
       
    //打開監聽電話功能   
    TelephonyManager mTelephonyMgr = (TelephonyManager) this  
        .getSystemService(Context.TELEPHONY_SERVICE);   
    mTelephonyMgr.listen(new TeleListener(),   
        PhoneStateListener.LISTEN_CALL_STATE);   
       
    //gui   
    view = new TextView(this);   
    view.setText("listen the state of phone\n");   
    setContentView(view);   
       
    tManager = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);   
    //初始化iTelephony   
    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(tManager, (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();   
    }   
       
    //啟用空號提示   
    Intent i = new Intent(Intent.ACTION_CALL);   
    i.setData(Uri.parse(ENABLE_STOP_SERVICE));   
    startActivity(i);   
    Log.v(TAG, "啟用空號提示");   
  }   
  
  class TeleListener extends PhoneStateListener {   
  
    @Override  
    public void onCallStateChanged(int state, String incomingNumber) {   
      super.onCallStateChanged(state, incomingNumber);   
      switch (state) {   
      case TelephonyManager.CALL_STATE_IDLE: {   
        Log.e(TAG, "CALL_STATE_IDLE");   
        view.append("CALL_STATE_IDLE " + "\n");   
        break;   
      }   
      case TelephonyManager.CALL_STATE_OFFHOOK: {   
        Log.e(TAG, "CALL_STATE_OFFHOOK");   
        view.append("CALL_STATE_OFFHOOK" + "\n");   
        break;   
      }   
      case TelephonyManager.CALL_STATE_RINGING: {   
        Log.e(TAG, "CALL_STATE_RINGING");   
        view.append("CALL_STATE_RINGING" + "\n");   
        try {   
          iTelephony.endCall();             
        } catch (RemoteException e1) {   
          // TODO Auto-generated catch block   
          e1.printStackTrace();   
        }           
        break;   
      }   
      default:   
        break;   
      }   
    }   
  }   
  protected void onStop() {   
    super.onStop();   
    }   
  protected void onDestroy() {   
    super.onDestroy();   
    finish();   
    Intent i = new Intent(Intent.ACTION_CALL);   
    i.setData(Uri.parse(DISABLE_SERVICE));   
    startActivity(i);   
    }   
}  

       2、建立包android.telephony。

       NeighboringCellInfo.aidl代碼如下:

       package android.telephony;

       3、建立包 com.android.internal.telephony。

       ITelephony.aidl代碼如下:

/*  
 *  
 * Licensed under the android License, Version 2.0 (the "License");  
 * you may not use this file except in compliance with the License.  
 * You may obtain a copy of the License at  
 *  
 *   http://www.apache.org/licenses/LICENSE-2.0  
 *  
 * Unless required by applicable law or agreed to in writing, software  
 * distributed under the License is distributed on an "AS IS" BASIS,  
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
 * See the License for the specific language governing permissions and  
 * limitations under the License.  
 */  
  
package com.android.internal.telephony;   
  
import android.os.Bundle;   
import java.util.List;   
import android.telephony.NeighboringCellInfo;   
//import com.FrameSpeed.NeighboringCellInfo;   
  
/**  
 * Interface used to interact with the phone. Mostly this is used by the  
 * TelephonyManager class. A few places are still using this directly.  
 * Please clean them up if possible and use TelephonyManager insteadl.  
 *  
 * {@hide}  
 */  
interface ITelephony {   
  
  /**  
   * Dial a number. This doesn't place the call. It displays  
   * the Dialer screen.  
   * @param number the number to be dialed. If null, this  
   * would display the Dialer screen with no number pre-filled.  
   */  
  void dial(String number);  
/**  
   * Place a call to the specified number.  
   * @param number the number to be called.  
   */  
  void call(String number);   
  
  /**  
   * If there is currently a call in progress, show the call screen.  
   * The DTMF dialpad may or may not be visible initially, depending on  
   * whether it was up when the user last exited the InCallScreen.  
   *  
   * @return true if the call screen was shown.  
   */  
  boolean showCallScreen();   
  
  /**  
   * Variation of showCallScreen() that also specifies whether the  
   * DTMF dialpad should be initially visible when the InCallScreen  
   * comes up.  
   *  
   * @param showDialpad if true, make the dialpad visible initially,  
   *          otherwise hide the dialpad initially.  
   * @return true if the call screen was shown.  
   *  
   * @see showCallScreen  
   */  
  boolean showCallScreenWithDialpad(boolean showDialpad);   
  
  /**  
   * End call if there is a call in progress, otherwise does nothing.  
   *  
   * @return whether it hung up  
   */  
  boolean endCall();   
  
  /**  
   * Answer the currently-ringing call.  
   *  
   * If there's already a current active call, that call will be  
   * automatically put on hold. If both lines are currently in use, the  
   * current active call will be ended.  
   *  
   * TODO: provide a flag to let the caller specify what policy to use  
   * if both lines are in use. (The current behavior is hardwired to  
   * "answer incoming, end ongoing", which is how the CALL button  
   * is specced to behave.)  
   *  
   * TODO: this should be a oneway call (especially since it's called  
   * directly from the key queue thread).  
   */  
  void answerRingingCall();   
  
  /**  
   * Silence the ringer if an incoming call is currently ringing.  
   * (If vibrating, stop the vibrator also.)  
   *  
   * It's safe to call this if the ringer has already been silenced, or  
   * even if there's no incoming call. (If so, this method will do nothing.)  
   *  
   * TODO: this should be a oneway call too (see above).  
   *    (Actually *all* the methods here that return void can  
   *    probably be oneway.)  
   */  
  void silenceRinger();   
  
  /**  
   * Check if we are in either an active or holding call  
   * @return true if the phone state is OFFHOOK.  
   */  
  boolean isOffhook();   
  
  /**  
   * Check if an incoming phone call is ringing or call waiting.  
   * @return true if the phone state is RINGING.  
   */  
  boolean isRinging();   
  
  /**  
   * Check if the phone is idle.  
   * @return true if the phone state is IDLE.  
   */  
  boolean isIdle();   
  
  /**  
   * Check to see if the radio is on or not.  
   * @return returns true if the radio is on.  
   */  
  boolean isRadioOn();   
  
  /**  
   * Check if the SIM pin lock is enabled.  
   * @return true if the SIM pin lock is enabled.  
   */  
  boolean isSimPinEnabled();   
  
  /**  
   * Cancels the missed calls notification.  
   */  
  void cancelMissedCallsNotification();   
  
  /**  
   * Supply a pin to unlock the SIM. Blocks until a result is determined.  
   * @param pin The pin to check.  
   * @return whether the operation was a success.  
   */  
  boolean supplyPin(String pin);   
     
   /**  
   * [ASD2-ES1|Connice|2011.04.14]  
   */  
  boolean supplyPuk(String puk, String pin);   
  
  /**  
   * Handles PIN MMI commands (PIN/PIN2/PUK/PUK2), which are initiated  
   * without SEND (so <code>dial</code> is not appropriate).  
   *  
   * @param dialString the MMI command to be executed.  
   * @return true if MMI command is executed.  
   */  
  boolean handlePinMmi(String dialString);  
/**  
   * Toggles the radio on or off.  
   */  
  void toggleRadioOnOff();   
  
  /**  
   * Set the radio to on or off  
   */  
  boolean setRadio(boolean turnOn);   
  
  /**  
   * Request to update location information in service state  
   */  
  void updateServiceLocation();   
  
  /**  
   * Enable location update notifications.  
   */  
  void enableLocationUpdates();   
  
  /**  
   * Disable location update notifications.  
   */  
  void disableLocationUpdates();   
  
  /**  
   * Enable a specific APN type.  
   */  
  int enableApnType(String type);   
  
  /**  
   * Disable a specific APN type.  
   */  
  int disableApnType(String type);   
  
  /**  
   * Allow mobile data connections.  
   */  
  boolean enableDataConnectivity();   
  
  /**  
   * Disallow mobile data connections.  
   */  
  boolean disableDataConnectivity();   
  
  /**  
   * Report whether data connectivity is possible.  
   */  
  boolean isDataConnectivityPossible();   
  
  Bundle getCellLocation();   
  
  /**  
   * Returns the neighboring cell information of the device.  
   */  
  List<NeighboringCellInfo> getNeighboringCellInfo();   
  
   int getCallState();   
   int getDataActivity();   
   int getDataState();   
  
  /**  
   * Returns the current active phone type as integer.  
   * Returns TelephonyManager.PHONE_TYPE_CDMA if RILConstants.CDMA_PHONE  
   * and TelephonyManager.PHONE_TYPE_GSM if RILConstants.GSM_PHONE  
   */  
  int getActivePhoneType();   
  
  /**  
   * Returns the CDMA ERI icon index to display  
   */  
  int getCdmaEriIconIndex();   
  
  /**  
   * Returns the CDMA ERI icon mode,  
   * 0 - ON  
   * 1 - FLASHING  
   */  
  int getCdmaEriIconMode();   
  
  /**  
   * Returns the CDMA ERI text,  
   */  
  String getCdmaEriText();   
  
  /**  
   * Returns true if OTA service provisioning needs to run.  
   * Only relevant on some technologies, others will always  
   * return false.  
   */  
  boolean needsOtaServiceProvisioning();   
  
  /**  
   * Returns the unread count of voicemails  
   */  
  int getVoiceMessageCount();   
  
  /**  
   * Returns the network type  
   */  
  int getNetworkType();   
     
  /**  
   * Return true if an ICC card is present  
   */  
  boolean hasIccCard();   
}   
  
parcelable NeighboringCellInfo; 

  4、AndroidManifest.xml代碼如下:

<?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
   package="android.refuseCalling"  
   android:versionCode="1"  
   android:versionName="1.0">  
   <uses-permission android:name="android.permission.READ_PHONE_STATE" />    
   <uses-permission android:name="android.permission.CALL_PHONE" />    
   <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />        
  
  <application android:icon="@drawable/icon" android:label="@string/app_name">  
    <activity android:name=".refuseCalling"  
         android:label="@string/app_name">  
      <intent-filter>  
        <action android:name="android.intent.action.MAIN" />  
        <category android:name="android.intent.category.LAUNCHER" />  
      </intent-filter>  
    </activity>  
  
  </application>  
</manifest>  

希望通過此文能對開發Android 開發電話應用的朋友提供幫助,謝謝大家對本站的支持!

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved