调用ITelephony的endCall( )方法自动挂断电话

各种来电防火墙之类的功能都可以过滤掉黑名单中的来电, 原理是响铃是判断来电号码是否存在于黑名单中, 如果存在则将其挂断. 而一些软件可以选择被挂断的家伙听到的提示音是"正在通话中"还是"欠费停机"之类的, 则是通过判断之后用运营商的"呼叫转移"功能将号码转接给一个已经停机的号码来实现的. 


挂断一个电话在API-10和之前版本中直接调用TelephonyManager对象的endCall方法即可, 但是之后的版本中这个API不再被公开(升级后的Android系统中还存在, 但是在Android SDK中不再提供给开发者* ). 


还想忤逆股沟的意思来调用它的话, 大概有两中方式: 

第一种: 自己定制android.jar文件. 这种方法不用每次用到"非公开"的API都在代码中费一番功夫. 这里http://mogoweb.net/archives/87 有非常详细的说明. 

第二种: 用反射. (有两种方法): 

package com.example.hanguptest;

import java.lang.reflect.Method;

import android.app.Activity;
import android.os.Bundle;
import android.os.IBinder;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Toast;

import com.android.internal.telephony.ITelephony;


public class MainActivity extends Activity {

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

	public void hangUp1(View v) throws Exception{
		Toast.makeText(getApplicationContext(), "hangUp1", 0).show();
		
		TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
		
		Class tmClazz = tm.getClass();
		
		Method getITelephonyMethod = tmClazz.getDeclaredMethod("getITelephony", null);
		getITelephonyMethod.setAccessible(true);
		
		ITelephony iTelephony = (ITelephony) getITelephonyMethod.invoke(tm, null);
		
		iTelephony.endCall();
	}	
	
	/**
	 * @param v
	 * @throws Exception
	 */
	public void hangUp2(View v) throws Exception{
		Toast.makeText(getApplicationContext(), "hangUp1", 0).show();
		
		Class clazz = Class.forName("android.os.ServiceManager");
		
		Method getServiceMethod = clazz.getMethod("getService", String.class);
		
		IBinder iBinder = (IBinder) getServiceMethod.invoke(null, TELEPHONY_SERVICE);
		
		ITelephony iTelephony = ITelephony.Stub.asInterface(iBinder);
		
		iTelephony.endCall();
	}	
	
}


-----------------------

注: * 在模拟器和手机上的system/framework/framework.jar中存在, 但是在SDK的sdk\platforms\android-X\android.jar中被移除了. 


----

看了这两篇文章, 收获也很大. 

http://blog.csdn.net/louiswangbing/article/details/6607418

http://www.linuxidc.com/Linux/2011-11/46416.htm


你可能感兴趣的:(调用ITelephony的endCall( )方法自动挂断电话)