在文章的开头先列出以下需要注意的地方:
1、需要用到的权限如下:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/><!--用于读取电话的状态--> <uses-permission android:name="android.permission.WRITE_CONTACTS"/><!--用于删除来电记录--> <uses-permission android:name="android.permission.CALL_PHONE"/><!--用于挂断电话-->
2、挂断电话所需要用到的类、方法、与包
类:
Class clazz = Class.forName("android.os.ServiceManager");
方法:
Method method = clazz.getMethod("getService", String.class);
包:
所需要用到的包的下载路径为:
下载地址如下:
http://download.csdn.net/detail/caihongshijie6/6282451
代码如下:
在写代码之前先把所需要用到的包导进来。。
1、MainActivity
package com.njupt.endcall1; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = new Intent(this,PhoneService1.class); startService(intent); } @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; } }
2、PhoneService
package com.njupt.endcall1; import java.lang.reflect.Method; import com.android.internal.telephony.ITelephony; import android.app.Service; import android.content.Context; import android.content.Intent; import android.database.ContentObserver; import android.net.Uri; import android.os.Handler; import android.os.IBinder; import android.provider.CallLog.Calls; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; public class PhoneService1 extends Service { private TelephonyManager tm; private MyPhoneStateListener listener; @Override public void onCreate() { super.onCreate(); tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); listener = new MyPhoneStateListener(); tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE); } private class MyPhoneStateListener extends PhoneStateListener { public MyPhoneStateListener() { } @Override public void onCallStateChanged(int state, String incomingNumber) { super.onCallStateChanged(state, incomingNumber); switch (state) { case TelephonyManager.CALL_STATE_IDLE: break; case TelephonyManager.CALL_STATE_OFFHOOK: break; case TelephonyManager.CALL_STATE_RINGING: endcall(incomingNumber); break; default: break; } } } private void endcall(String incomingNumber) { try { if (incomingNumber.equals("5560")) { Class clazz = Class.forName("android.os.ServiceManager"); Method method = clazz.getMethod("getService", String.class); IBinder ibinder = (IBinder) method.invoke(null, Context.TELEPHONY_SERVICE); ITelephony iTelephony = ITelephony.Stub.asInterface(ibinder); iTelephony.endCall(); getContentResolver().registerContentObserver(Calls.CONTENT_URI,true, new MyContentObserver(new Handler(), incomingNumber)); } } catch (Exception e) { e.printStackTrace(); } } private class MyContentObserver extends ContentObserver{ private String incomingNumber; public MyContentObserver(Handler handler , String incomingNumber) { super(handler); this.incomingNumber = incomingNumber; } @Override public void onChange(boolean selfChange) { super.onChange(selfChange); Uri uri = Calls.CONTENT_URI; getContentResolver().delete(uri," number = ? ", new String[]{incomingNumber}); getContentResolver().unregisterContentObserver(this); } } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } }
3、AndroidManifest.xml
<uses-permission android:name="android.permission.READ_PHONE_STATE"/> <uses-permission android:name="android.permission.WRITE_CONTACTS"/> <uses-permission android:name="android.permission.CALL_PHONE"/>
<service android:name="com.njupt.endcall1.PhoneService1"/>