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

我想实现一个应用程序阻止手机号码接收或发送电话和消息。 在我的应用程序中,我在EditText框中input手机号码,然后点击一个button来阻止用户input的手机号码。

我已经实施了一个活动课程如下:

 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 。 但是我没有更多的知识。 请给我一个想法如何实施阻止或解锁手机号码。 请任何身体帮助我…..

创buildPhoneCallReceiver .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); }} 

现在创buildPhoneCallStateListener .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中创build这个 com.android.internal.telephony 现在在这个包右键单击 – >新build – >文件现在给名称ITelephony.aidl并粘贴此代码

 package com.android.internal.telephony; interface ITelephony { boolean endCall(); void answerRingingCall(); void silenceRinger(); } 

注意:代码在Android 2.2(Froyo),2.3(GingerBread)

阻止来电使用这个http://androidsourcecode.blogspot.in/2010/10/blocking-incoming-call-android.html也是这个http://www.codeproject.com/Questions/333253/Block-Incoming-在Android中不使用单一的RING , http: //www.anddev.org/post52643.html?hilit = incoming%20call#p52643或阻止传出请参阅如何阻止传出呼叫和短信和阻止传出呼叫和文本(BroadcastReceiver或Service?) , Android:完全控制电话(Kiosk模式),有可能吗? 怎么样?

为了打电话给任何特定的联系号码,那么这个教程将帮助你解决你的问题。 http://androiddesk.wordpress.com/2012/08/02/blocking-a-call-without-user-intervention-in-android/

此代码适用于我

  try { String serviceManagerName = "android.os.ServiceManager"; String serviceManagerNativeName = "android.os.ServiceManagerNative"; String telephonyName = "com.android.internal.telephony.ITelephony"; Class<?> telephonyClass; Class<?> telephonyStubClass; Class<?> serviceManagerClass; Class<?> serviceManagerNativeClass; Method telephonyEndCall; Object telephonyObject; Object serviceManagerObject; telephonyClass = Class.forName(telephonyName); telephonyStubClass = telephonyClass.getClasses()[0]; serviceManagerClass = Class.forName(serviceManagerName); serviceManagerNativeClass = Class.forName(serviceManagerNativeName); Method getService = serviceManagerClass.getMethod("getService", String.class); Method tempInterfaceMethod = serviceManagerNativeClass.getMethod( "asInterface", IBinder.class); Binder tmpBinder = new Binder(); tmpBinder.attachInterface(null, "fake"); serviceManagerObject = tempInterfaceMethod.invoke(null, tmpBinder); IBinder retbinder = (IBinder) getService.invoke( serviceManagerObject, "phone"); Method serviceMethod = telephonyStubClass.getMethod("asInterface", IBinder.class); telephonyObject = serviceMethod.invoke(null, retbinder); telephonyEndCall = telephonyClass.getMethod("endCall"); telephonyEndCall.invoke(telephonyObject); } } catch (Exception e) { e.printStackTrace(); Toast.makeText(context, "Unable to Block Call", Toast.LENGTH_SHORT).show(); } 

此代码正在工作…

首先下载这个类ITelephony

 if ((state != null) && (state.equals(TelephonyManager.EXTRA_STATE_RINGING))) { CallLogReceiver.phoneNo = intent.getExtras().getString( "incoming_number"); if (CallLogReceiver.blockNo.equals(CallLogReceiver.phoneNo)) { Intent blockCallIntent = new Intent(); blockCallIntent.setClassName("com.example.calllogdemo", "com.example.calllogdemo.BlockCallActivity"); blockCallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); blockCallIntent .putExtra("blockNo", CallLogReceiver.phoneNo); context.startActivity(blockCallIntent); try { TelephonyManager telephonyManager = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); Class<?> classTelephony = Class .forName(telephonyManager.getClass().getName()); Method methodGetITelephony = classTelephony .getDeclaredMethod("getITelephony"); methodGetITelephony.setAccessible(true); ITelephony telephonyService = (ITelephony) methodGetITelephony .invoke(telephonyManager); telephonyService.endCall(); } catch (Exception e) { e.printStackTrace(); } } } 

并在清单文件中使用此权限

  <uses-permission android:name="android.permission.CALL_PHONE" /> 
Interesting Posts