Android 立即拒绝来电
我们能在android手机进入手机应用程序之前阻止来电吗? 我已经做了一个阻止来电的应用程序,我的问题是,即使电话立即挂起,它也会弹出一秒钟的通话屏幕。有什么建议可以避免这种低效? 另外,如果有办法从通话记录中删除电话,请告诉我如何删除 这是我的主要活动Android 立即拒绝来电,android,Android,我们能在android手机进入手机应用程序之前阻止来电吗? 我已经做了一个阻止来电的应用程序,我的问题是,即使电话立即挂起,它也会弹出一秒钟的通话屏幕。有什么建议可以避免这种低效? 另外,如果有办法从通话记录中删除电话,请告诉我如何删除 这是我的主要活动 import android.Manifest; import android.content.pm.PackageManager; import android.support.v4.app.ActivityCompa
import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
private static final int PERMISSION_REQUEST_PHONE_STATE_GRANTED = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getPermission();
}
private void getPermission() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_PHONE_STATE) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.READ_PHONE_STATE},
PERMISSION_REQUEST_PHONE_STATE_GRANTED);
}
}
}
这是我的CallReceiver类,它扩展了BroadcastReceiver
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import com.android.internal.telephony.ITelephony;
import java.lang.reflect.Method;
public class CallReceiver extends BroadcastReceiver {
private ITelephony telephonyService;
@Override
public void onReceive(Context context, Intent intent) {
TelephonyManager telephony = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
//make sure it's not an outgoing call
if (telephony.getCallState() == 1) {
try {
Class c = Class.forName(telephony.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(telephony);
telephonyService.endCall();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
这是我的清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shulem.phonecallsblocker">
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission
android:name="android.permission.READ_PHONE_STATE"/>
<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=".CallReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action
android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
</application>
</manifest>
Ps
我尝试添加android优先级,但没有帮助 依靠电话状态来确定是否有来电,然后通过TelephonyManager结束来电,这绝不是实现这种性质的来电筛选应用程序的可靠方法。电话状态的广播是异步发送的,而系统正忙于执行与呼叫设置相关的所有其他事情。因此,在某些设备上,它可能会工作,但在其他设备上,它很可能不会工作