【问题标题】:Incoming Call Blocking When App is Open应用程序打开时阻止来电
【发布时间】:2017-08-24 04:40:48
【问题描述】:

我已经成功实现了阻塞调用的代码,但是它即使我关闭了应用程序也会阻塞所有调用。我需要在打开特定活动时阻止呼叫(如果在应用程序打开时不可能阻止呼叫也可以)。我在下面附上我的代码。请通过它。如有任何积极回应,请提前致谢。

BlockCallReceiver.java

import java.lang.reflect.Method;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;

public class BlockCallReceiver extends BroadcastReceiver{
    Context context;
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Bundle myBundle = intent.getExtras();
        if (myBundle != null)
        {
            System.out.println("--------Not null-----");
            try
            {
                if (intent.getAction().equals("android.intent.action.PHONE_STATE"))
                {
                    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
                    System.out.println("--------in state-----");
                    if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
                    {
                        // Incoming call
                        String incomingNumber =intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
                        System.out.println("--------------my number---------"+incomingNumber);

// this is main section of the code,. could also be use for particular number.
                        // Get the boring old TelephonyManager.
                        TelephonyManager telephonyManager =(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

                        // Get the getITelephony() method
                        Class<?> classTelephony = Class.forName(telephonyManager.getClass().getName());
                        Method methodGetITelephony = classTelephony.getDeclaredMethod("getITelephony");

                        // Ignore that the method is supposed to be private
                        methodGetITelephony.setAccessible(true);

                        // Invoke getITelephony() to get the ITelephony interface
                        Object telephonyInterface = methodGetITelephony.invoke(telephonyManager);

                        // Get the endCall method from ITelephony
                        Class<?> telephonyInterfaceClass = Class.forName(telephonyInterface.getClass().getName());
                        Method methodEndCall = telephonyInterfaceClass.getDeclaredMethod("endCall");

                        // Invoke endCall()
                        methodEndCall.invoke(telephonyInterface);

                    }

                }
            }
            catch (Exception ex)
            { // Many things can go wrong with reflection calls
                ex.printStackTrace();
            }
        }
    }
}

MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.karan.churi.PermissionManager.PermissionManager;

import java.lang.reflect.Method;

public class MainActivity extends AppCompatActivity {
    PermissionManager permission;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        permission=new PermissionManager() {};
        permission.checkAndRequestPermissions(this);
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.arjun.myapplication">
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-feature
        android:name="android.hardware.telephony"
        android:required="false" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".BlockCallReceiver" >
            <intent-filter android:priority="100" >
                <action android:name="android.intent.action.PHONE_STATE" >
                </action>
            </intent-filter>
        </receiver>
    </application>

</manifest>

【问题讨论】:

    标签: android telephonymanager callblocking


    【解决方案1】:

    您需要在您的MainActivity 中初始化一个BroadcastReceiver,如下所示。

    private final BroadcastReceiver mBloackCallReceiver = new BlockCallReceiver();
    

    然后在您的活动的onCreate 函数中使用以下代码注册BroadcastReceiver

    registerReceiver(mBloackCallReceiver , new IntentFilter(TelephonyManager.ACTION_PHONE_STATE_CHANGED));
    

    请记住在您的活动的onDestroy 函数中使用以下代码取消注册BroadcastReceiver

    unregisterReceiver(mybroadcast);
    

    从您的AndroidManifest.xml 中删除receiver 标签。

    【讨论】:

    • 从清单中删除了接收器标签,工作完美。谢谢朋友
    • 只是提醒一下,onDestroy 不能保证在活动退出时被调用,你应该把你的 registerReceiver 放在onResume,把 unregisterReceiver 放在onPause 以确保你总是在之后清理自己
    【解决方案2】:

    我已经在我的应用程序类中添加了这段代码,并使用计时器每 10 秒调用一次这个方法。我希望这会对你有所帮助。试试这个代码:

        public  boolean isAppIsInBackground(Context context) {
        boolean isInBackground = true;
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
            List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
            for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
                if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                    for (String activeProcess : processInfo.pkgList) {
                        if (activeProcess.equals(context.getPackageName())) {
                            isInBackground = false;
                        }
                    }
                }
            }
        } else {
            List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
            ComponentName componentInfo = taskInfo.get(0).topActivity;
            if (componentInfo.getPackageName().equals(context.getPackageName())) {
                isInBackground = false;
            }
        }
    
        Log.i(TAG, "isAppIsInBackground: "+isInBackground);
        return isInBackground;
    }
    

    【讨论】:

    • 我知道解决方案,如果我从活动而不是清单中注册和注销此广播接收器,问题将得到解决,但我不知道在这种情况下是如何完成的。
    • 实际上我实现了这段代码来检查应用程序是在前台还是后台,并据此将我的数据发送到服务器。我想你也有同样的问题,这就是我与你分享我的代码的原因.
    • 是的,我的应用程序不会进入后台,因此应用程序后台条件是不可能的。我选择这个呼叫块,因为在某些手机中,呼叫会像弹出窗口一样排在​​首位。它将使应用程序保持在前台状态,但用户可以通过此弹出窗口接听电话
    猜你喜欢
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    • 2015-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多