zoukankan      html  css  js  c++  java
  • 挂断电话的实现(即类似于*黑名单)

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

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




  • 相关阅读:
    关于VSCode如何缩进两个空格
    基于vue来开发一个仿饿了么的外卖商城(二)
    在复杂的项目开发中使用结对编程
    第一个博客
    docker容器启动失败解决办法
    windows转mac-开发环境搭建(六):mac上搭建git环境
    windows转mac-开发环境搭建(五):mac上用docker安装并运行mysql
    windows转mac-开发环境搭建(四):mac上搭建node、VS code、idea环境
    windows转mac-开发环境搭建(三):mac上搭建maven环境
    windows转mac-开发环境搭建(二):mac上搭建java环境
  • 原文地址:https://www.cnblogs.com/james1207/p/3327699.html
Copyright © 2011-2022 走看看