我在manifest.xml中使用权限(CALL_PHONE,READ_PHONE_STATE,PROCESS_INCOMING_CALLS,MODIFY_PHONE_STATE),ITelephony.aids我在博客中显示此代码。 在ITelephony.aids我写 我想在选中复选框时禁用所有来电。代码不返回错误但不起作用。我在android 6.0中测试它。请帮帮我
CheckBox blockAll_cb;//,blockcontacts_cb;
BroadcastReceiver CallBlocker;
TelephonyManager telephonyManager;
ITelephony telephonyService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_call_controller);
initviews();
blockAll_cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d("Error 8","Is Checked1");
// TODO Auto-generated method stub
CallBlocker =new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent) {
Log.d("Error 8","Is checked2");
// TODO Auto-generated method stub
telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
//Java Reflections
Class c = null;
try {
c = Class.forName(telephonyManager.getClass().getName());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("Error 1",""+e);
}
Method m = null;
try {
m = c.getDeclaredMethod("getITelephony");
} catch (SecurityException e) {
// TODO Auto-generated catch block
Log.d("Error 2",""+e);
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
Log.d("Error 3",""+e);
e.printStackTrace();
}
m.setAccessible(true);
try {
telephonyService = (ITelephony)m.invoke(telephonyManager);
} catch (IllegalArgumentException e) {
Log.d("Error 4",""+e);
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
Log.d("Error 5",""+e);
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
Log.d("Error 6",""+e);
// TODO Auto-generated catch block
e.printStackTrace();
}
telephonyManager.listen(callBlockListener, PhoneStateListener.LISTEN_CALL_STATE);
}//onReceive()
PhoneStateListener callBlockListener = new PhoneStateListener()
{
public void onCallStateChanged(int state, String incomingNumber)
{
if(state==TelephonyManager.CALL_STATE_RINGING)
{
if(blockAll_cb.isChecked())
{
try {
telephonyService.endCall();
Log.d("Error End","EndCall");
} catch (RemoteException e) {
Log.d("Error 7",""+e);
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
};
};//BroadcastReceiver
IntentFilter filter= new IntentFilter("android.intent.action.PHONE_STATE");
registerReceiver(CallBlocker, filter);
}
});
}
public void initviews()
{
blockAll_cb=(CheckBox)findViewById(R.id.cbBlockAll);
//blockcontacts_cb=(CheckBox)findViewById(R.id.cbBlockContacts);
}
@Override
protected void onDestroy() {
Log.d("Error 8","Error 8");
// TODO Auto-generated method stub
super.onDestroy();
}
}