我正面临 android 呼叫阻止选项的问题。我正在制作一个应用程序,我想在其中为用户提供阻止不需要的呼叫。我已经用谷歌搜索了它,但找到了一个在我的情况下不起作用的解决方案。我想补充一点,我正在用我的模拟器尝试这个 - 模拟器呼叫服务。我的一个模拟器的编号是 15555215556,另一个是 15555215554。请帮我写代码。我在这里发布我遇到的错误。
02-22 23:44:05.769: E/BroadcastReceiver(377): BroadcastReceiver trying to return result during a non-ordered broadcast
02-22 23:44:05.769: E/BroadcastReceiver(377): java.lang.RuntimeException: BroadcastReceiver trying to return result during a non-ordered broadcast
02-22 23:44:05.769: E/BroadcastReceiver(377): at android.content.BroadcastReceiver.checkSynchronousHint(BroadcastReceiver.java:451)
02-22 23:44:05.769: E/BroadcastReceiver(377): at android.content.BroadcastReceiver.setResultData(BroadcastReceiver.java:266)
02-22 23:44:05.769: E/BroadcastReceiver(377): at com.example.CallReceiver.onReceive(CallReceiver.java:33)
02-22 23:44:05.769: E/BroadcastReceiver(377): at android.app.ActivityThread.handleReceiver(ActivityThread.java:1794)
02-22 23:44:05.769: E/BroadcastReceiver(377): at android.app.ActivityThread.access$2400(ActivityThread.java:117)
02-22 23:44:05.769: E/BroadcastReceiver(377): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:981)
02-22 23:44:05.769: E/BroadcastReceiver(377): at android.os.Handler.dispatchMessage(Handler.java:99)
02-22 23:44:05.769: E/BroadcastReceiver(377): at android.os.Looper.loop(Looper.java:123)
02-22 23:44:05.769: E/BroadcastReceiver(377): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-22 23:44:05.769: E/BroadcastReceiver(377): at java.lang.reflect.Method.invokeNative(Native Method)
02-22 23:44:05.769: E/BroadcastReceiver(377): at java.lang.reflect.Method.invoke(Method.java:507)
02-22 23:44:05.769: E/BroadcastReceiver(377): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-22 23:44:05.769: E/BroadcastReceiver(377): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-22 23:44:05.769: E/BroadcastReceiver(377): at dalvik.system.NativeStart.main(Native Method)
这是我的代码:
package com.example;
import java.lang.reflect.Method;
import com.android.internal.telephony.ITelephony;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
public class CallReceiver extends BroadcastReceiver{
private String incomingnumber;
@Override
public void onReceive(Context context, Intent intent) {
String s[]={"15555215554","15555215556"};
Bundle b = intent.getExtras();
incomingnumber = b.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
//
try {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
com.android.internal.telephony.ITelephony telephonyService = (ITelephony) m.invoke(tm);
for (int i = 0; i < s.length; i++) {
if(s[i].equals(incomingnumber)){
// telephonyService.endCall();
Log.e("incoming:",incomingnumber);
}
}
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
Log.e("incoming:","hide");
}
}
}
电话接口:
package com.android.internal.telephony;
public interface ITelephony
{
boolean endCall();
void answerRingingCall();
}
我只提供了一种使用许可,即: <uses-permission android:name="android.permission.READ_PHONE_STATE" />