阻止来电和拨出电话?

发布于 2024-11-28 14:28:51 字数 81 浏览 4 评论 0原文

我想创建一个应用程序,我可以在阻止或允许列表中设置不同的电话号码。是否可以阻止或仅允许某些电话号码,如果可以,我将如何执行此操作。预先感谢,乔纳森。

I would like to create a app that i can be able to set different phone numbers in a block or allow list. Is it possible to block or allow only certain phone numbers and if so how would i go about doing this. Thanks in advance, Jonathan.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只是在用心讲痛 2024-12-05 14:28:51

听起来您必须创建一个带有注册 PhoneStateListener 的服务,该服务使您能够侦听有关电话状态更改的事件。因此,如果有人打电话,您可以在侦听器中捕获该事件并做出相应反应。您自己的 PhoneStateListener 可能如下所示:

public class CustomPhoneStateListener extends PhoneStateListener {

public void onCallStateChanged(int state, String incomingNumber) {

    switch (state) {
    case TelephonyManager.CALL_STATE_IDLE:            
        doSomething();
        break;
    case TelephonyManager.CALL_STATE_OFFHOOK:
        doSomething();
        break;
    case TelephonyManager.CALL_STATE_RINGING:
        if(incomingNumber.equels(blockedNumber)) {
            blockNumber();
        }
        break;
    }
}

}

在电话状态等于 CALL_STATE_RINGING 的情况下,如果来电号码等于“阻止号码列表”中的号码,您可以放置​​逻辑来阻止来电。要使其全部正常工作,您必须在服务中注册该侦听器(在 onCreate 方法中,并且不要忘记在 onDestroy 中取消注册),并向您的 android 清单添加一个权限,使您能够读取手机状态。

      <uses-permission android:name="android.permission.READ_PHONE_STATE" />

Sounds like you have to create a Service with a registered PhoneStateListener which enables you to listen to events regarding the change of the phone state. So if someone calls you can catch that event in your listener and react accordingly. Your own PhoneStateListener could look like this:

public class CustomPhoneStateListener extends PhoneStateListener {

public void onCallStateChanged(int state, String incomingNumber) {

    switch (state) {
    case TelephonyManager.CALL_STATE_IDLE:            
        doSomething();
        break;
    case TelephonyManager.CALL_STATE_OFFHOOK:
        doSomething();
        break;
    case TelephonyManager.CALL_STATE_RINGING:
        if(incomingNumber.equels(blockedNumber)) {
            blockNumber();
        }
        break;
    }
}

}

In the case where phone state equals CALL_STATE_RINGING you can place your logic to block the incoming call if the incoming number equals a number in your "blocked numbers list". To get it all working you have to register that listener in your service (in the onCreate method and don't forget to unregister it in onDestroy) and also add a permission to your android manifest which enables you to read the phone state.

      <uses-permission android:name="android.permission.READ_PHONE_STATE" />
稚然 2024-12-05 14:28:51

首先创建一个广播接收器:
1)添加到清单

2)接收器:

public class CallReciever extends BroadcastReceiver 
{

@Override
public void onReceive(Context context, Intent intent) 
{
    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
    ArrayList<String> rejectList=getListFromDb()//you need to implement this method
    if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
    {
        String num = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
        if (rejectList.Contains(num))
        {
            disconnectCall();


        }
    }
 public void disconnectCall(){
     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 = // getDefaults[29];
        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();
        Log.e("GABI",
                "FATAL ERROR: could not connect to telephony subsystem");
        Log.e("GABI", "Exception object: " + e); 
     }
    }

}

现在只有在您至少启动应用程序一次后,这才会起作用。
(您可以使用另一个将调用 startActivity 的接收器来调用您的应用程序以在启动时启动服务)
其权限和声明是:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name="com.my.CustomReceiver">  
    <intent-filter>  
        <action android:name="android.intent.action.BOOT_COMPLETED" />  
    </intent-filter>  
</receiver> 

first thing create a broadcast reciever:
1)add to manifest

2)the reciever:

public class CallReciever extends BroadcastReceiver 
{

@Override
public void onReceive(Context context, Intent intent) 
{
    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
    ArrayList<String> rejectList=getListFromDb()//you need to implement this method
    if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
    {
        String num = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
        if (rejectList.Contains(num))
        {
            disconnectCall();


        }
    }
 public void disconnectCall(){
     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 = // getDefaults[29];
        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();
        Log.e("GABI",
                "FATAL ERROR: could not connect to telephony subsystem");
        Log.e("GABI", "Exception object: " + e); 
     }
    }

}

now this will only work once you started your app at least once.
(you can call your app to start a service on boot using another reciever that will call startActivity)
the permisions and declaration for it are:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name="com.my.CustomReceiver">  
    <intent-filter>  
        <action android:name="android.intent.action.BOOT_COMPLETED" />  
    </intent-filter>  
</receiver> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文