如何在Android应用程序开发中阻止手机号码呼叫和消息接收?

我想实现一个用于阻止移动号码的应用程序,用于接收或发送呼叫和消息.在我的应用程序中,我在EditText框中输入手机号码然后我点击一个按钮来阻止用户输入的手机号码.

我已经实现了一个活动类,如下所示:

public class BlockNumberActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ((Button)findViewById(R.id.block)).setonClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String mobileNumer = ((EditText)findViewById(R.id.mobileNum)).getText().toString();
                //How to block entered mobileNumber
            }
        });

        ((Button)findViewById(R.id.unblock)).setonClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String mobileNumer = ((EditText)findViewById(R.id.mobileNum)).getText().toString();
                //How to unblock entered mobileNumber
            }
        });
    }
}

我想我们可以使用broadcastReceiver.但我对它没有更多的了解.请告诉我如何实现阻止或解锁移动号码.
请任何人帮我…..

解决方法:

创建PhoneCallReceiver .java

import android.content.broadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhonestateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
public class PhoneCallReceiver extends broadcastReceiver {  

@Override
public void onReceive(Context context, Intent intent) { 
    TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    PhoneCallStateListener customPhoneListener = new PhoneCallStateListener(context);
    telephony.listen(customPhoneListener, PhonestateListener.LISTEN_CALL_STATE);



}}

现在创建PhoneCallStateListener .java

import java.lang.reflect.Method;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.PhonestateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;

import com.android.internal.telephony.ITelephony;

public class PhoneCallStateListener extends PhonestateListener {    

private Context context;
public PhoneCallStateListener(Context context){
    this.context = context;
}


@Override
public void onCallStateChanged(int state, String incomingNumber) {  
    SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(context);

    switch (state) {

        case TelephonyManager.CALL_STATE_RINGING:       

              String block_number = prefs.getString("block_number", null);
            AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUdio_SERVICE); 
            //Turn ON the mute
            audioManager.setStreamMute(AudioManager.STREAM_RING, true);                 
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            try {
                Toast.makeText(context, "in"+block_number, Toast.LENGTH_LONG).show();
                Class clazz = Class.forName(telephonyManager.getClass().getName());
                Method method = clazz.getDeclaredMethod("getITelephony");
                method.setAccessible(true);
                ITelephony telephonyService = (ITelephony) method.invoke(telephonyManager);     
                //Checking incoming call number
                System.out.println("Call "+block_number);

                if (incomingNumber.equalsIgnoreCase("+91"+block_number)) {
                    //telephonyService.silenceRinger();//Security exception problem
                     telephonyService = (ITelephony) method.invoke(telephonyManager);
                     telephonyService.silenceRinger();
                    System.out.println(" in  "+block_number);
                    telephonyService.endCall();
                }
            } catch (Exception e) {
                Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
            }
            //Turn OFF the mute     
            audioManager.setStreamMute(AudioManager.STREAM_RING, false);
            break;
        case PhonestateListener.LISTEN_CALL_STATE:

    }
    super.onCallStateChanged(state, incomingNumber);
}}

现在在src中创建此包com.android.internal.telephony现在在此包中右键单击 – >新 – >文件现在命名为ITelephony.aidl并粘贴此代码

package com.android.internal.telephony; 

interface ITelephony {      

  boolean endCall();     

  void answerRingingCall();      

  void silenceRinger(); 
}

注意:代码在Android 2.2(Froyo),2.3(GingerBread)中进行测试

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...