挂断电话的实现(即类似于电话号码黑名单)

在文章的开头先列出以下需要注意的地方:

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"/>




评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值