首次使用复选框运行时阻止来电

时间:2014-05-27 08:04:18

标签: android checkbox

我想构建阻止传入来电的应用。我有一个复选框。如果选中条件中的复选框,则所有来电都将被阻止。当应用程序第一次运行时,复选框状态已选中。但它不起作用,它无法阻止来电。但是,当我取消选中并再次检查它时,它会起作用。那么,它第一次运行时如何工作??

这是我的代码:

public class MainActivity extends Activity {

 CheckBox blockAll_cb;//,blockcontacts_cb;

 TextView teks;

 BroadcastReceiver CallBlocker;
 TelephonyManager telephonyManager;
 ITelephony telephonyService;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    initviews();
    blockAll_cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

     @Override
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
     // TODO Auto-generated method stub
     CallBlocker =new BroadcastReceiver()
     {
     @Override
     public void onReceive(Context context, Intent intent) {
     // 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();
     }
     Method m = null;
     try {
     m = c.getDeclaredMethod("getITelephony");
     } catch (SecurityException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     } catch (NoSuchMethodException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     }
     m.setAccessible(true);
     try {
     telephonyService = (ITelephony)m.invoke(telephonyManager);
     } catch (IllegalArgumentException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     } catch (IllegalAccessException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     } catch (InvocationTargetException 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)

     {

         teks=(TextView)findViewById(R.id.title);

         teks.setText(incomingNumber);




     if(state==TelephonyManager.CALL_STATE_RINGING)
     {
     if(blockAll_cb.isChecked())
     {
     try {
     telephonyService.endCall();
     } catch (RemoteException 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() {
     // TODO Auto-generated method stub
     super.onDestroy();
     if (CallBlocker != null)
     {
     unregisterReceiver(CallBlocker);
     CallBlocker = null;
     }
     }
    }

1 个答案:

答案 0 :(得分:0)

将initviews()修改为:

 public void initviews()
     {
     blockAll_cb=(CheckBox)findViewById(R.id.cbBlockAll);
     //blockcontacts_cb=(CheckBox)findViewById(R.id.cbBlockContacts);
        blockAll_cb.setChecked(true)
     }

在您的xml文件中添加android:checked="true"。 然后:

  if(blockAll_cb.isChecked()==true)
{
  //write call blocking code here
}
else{
  //unblock the calls
}