打开应用程序时阻止来电
Incoming Call Blocking When App is Open
我已经成功实现了阻止调用的代码,但是它即使我关闭了应用程序也会阻止所有调用。我需要在打开特定 activity 时阻止调用(如果在应用程序打开时阻止调用也是不可能的)。我在下面附上我的代码。请通过它。对于任何积极的回应,提前致谢。
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>
我在我的应用程序中添加了这段代码 class 并使用 timer.I hopw 每 10 秒调用一次此方法这将有助于 you.Try 此代码:
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;
}
您需要像下面这样在 MainActivity
中初始化一个 BroadcastReceiver
。
private final BroadcastReceiver mBloackCallReceiver = new BlockCallReceiver();
然后在 activity 的 onCreate
函数中使用下面的代码注册 BroadcastReceiver
。
registerReceiver(mBloackCallReceiver , new IntentFilter(TelephonyManager.ACTION_PHONE_STATE_CHANGED));
记得在 activity 的 onDestroy
函数中使用下面的代码注销 BroadcastReceiver
。
unregisterReceiver(mybroadcast);
从您的 AndroidManifest.xml
中删除 receiver
标签。
我已经成功实现了阻止调用的代码,但是它即使我关闭了应用程序也会阻止所有调用。我需要在打开特定 activity 时阻止调用(如果在应用程序打开时阻止调用也是不可能的)。我在下面附上我的代码。请通过它。对于任何积极的回应,提前致谢。
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>
我在我的应用程序中添加了这段代码 class 并使用 timer.I hopw 每 10 秒调用一次此方法这将有助于 you.Try 此代码:
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;
}
您需要像下面这样在 MainActivity
中初始化一个 BroadcastReceiver
。
private final BroadcastReceiver mBloackCallReceiver = new BlockCallReceiver();
然后在 activity 的 onCreate
函数中使用下面的代码注册 BroadcastReceiver
。
registerReceiver(mBloackCallReceiver , new IntentFilter(TelephonyManager.ACTION_PHONE_STATE_CHANGED));
记得在 activity 的 onDestroy
函数中使用下面的代码注销 BroadcastReceiver
。
unregisterReceiver(mybroadcast);
从您的 AndroidManifest.xml
中删除 receiver
标签。