广播接收器所选号码/呼叫者的呼叫转移不起作用

时间:2014-02-14 08:44:28

标签: android broadcastreceiver

我创建了一个将所选呼叫转发给某个号码的应用。但是,现在,所有呼叫都转发到该号码。我的代码中缺少哪些内容,不允许转发特定的呼叫者。感谢

java class

public class CallForwarding extends Activity {

private SharedPreferences prefs;
private EditText editText;

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

final Switch switch1 = (Switch) findViewById(R.id.switch1);
editText = (EditText) findViewById(R.id.editText1);

SharedPreferences prefs = getPreferences(MODE_PRIVATE);
editText.setText(prefs.getString("edittext_content", ""));

switch1.setChecked(false);
switch1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    if (isChecked) {
        if(editText.getText().toString().trim().length() <= 7){
            switch1.setChecked(false);
            Toast.makeText(getApplicationContext(), "Please enter a number for receiving forwarded calls",Toast.LENGTH_SHORT).show();

        }else{
            Toast.makeText(getApplicationContext(), "Call Forwarding is activated",Toast.LENGTH_SHORT).show();
            callforward("*21*" + editText.getText().toString() + "#" ); // 0123456789 is the number you want to forward the calls.; 
        }
    }else{
        Toast.makeText(getApplicationContext(), "Call Forwarding is deactivated",Toast.LENGTH_SHORT).show();
           callforward("#21#");


    }
    }
});}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

public void onPause(){
    super.onPause();
    prefs = getPreferences(MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    editText = (EditText)findViewById(R.id.editText1);
    editor.putString("edittext_content", editText.getText().toString());
    editor.commit();
}

public void manage_numbers_onClick(View view) {
    Intent myIntent = new Intent(this, Set_numbers_to_forward.class);
    startActivity(myIntent);
  } 

private void callforward(String ArrayString)
{

    PhoneCallListener phoneListener = new PhoneCallListener();
    TelephonyManager telephonyManager = (TelephonyManager)
    this.getSystemService(Context.TELEPHONY_SERVICE);
    telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);

    Intent intentCallForward = new Intent(Intent.ACTION_CALL);
    Uri mmiCode = Uri.fromParts("tel", ArrayString, "#");
    intentCallForward.setData(mmiCode);
    startActivity(intentCallForward);
}

public void onReceive(Context context, Intent intent) {

String s = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

if(s.equals(TelephonyManager.EXTRA_STATE_RINGING)){
String incomingnumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
incomingnumber.contentEquals(incomingnumber);
}

}

  private class PhoneCallListener extends PhoneStateListener 
 {      private Context mContext;
    private boolean isPhoneCalling = false;
    DBAdapter db = new DBAdapter(mContext);
    @Override
    public void onCallStateChanged(int state, String incomingnumber) 
    {

        //phone ringing
        if (TelephonyManager.CALL_STATE_RINGING == state)
        { 
            String contacts = db.getAllContacts().toString();
                if(incomingnumber == contacts) {
                callforward("*21*" + editText.getText().toString() + "#" );
            } else if(incomingnumber != contacts){
                //do nothing
                callforward("#21#");
            }

        }

        if (TelephonyManager.CALL_STATE_OFFHOOK == state) 
        {
            // active
            isPhoneCalling = true;
        }

        if (TelephonyManager.CALL_STATE_IDLE == state) 
        {
            // run when class initial and phone call ended, need detect flag
            // from CALL_STATE_OFFHOOK
            if (isPhoneCalling)
            {
                Intent i = getBaseContext().getPackageManager()
                        .getLaunchIntentForPackage(getBaseContext().getPackageName());
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
                isPhoneCalling = false;
            }
        }
    }
 }

}

0 个答案:

没有答案