匹配被叫号码的方法

way to match the called number

我正在申请。

,按Button获取通讯录的phone个号码并保存

然后,每次来电时,它都会检查保存的phone号码,如果不在地址簿中就会发出警告。

使用realm,保存通讯录phone号码成功。 但是,如果我在真机上来电,应用程序将被强制终止。

不知道是不是验证phone号码的方法不对,保存的phone号码取不出来,还是太费时间了。 给我建议改进

MainActivity.java

public class MainActivity extends AppCompatActivity {

PhoneReceiver phoneStateListener;
TelephonyManager manager;

public Button getPhoneList;

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

    phoneStateListener = new PhoneReceiver(this);
    manager = ((TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE));

    getPhoneList = (Button)this.findViewById(R.id.phoneList);
    getPhoneList.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CreatePhoneNumbers();
        }
    });
  }

public void CreatePhoneNumbers () {
    final Cursor addressTable = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
    RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this).build();
    Realm realm = Realm.getInstance(realmConfiguration);
    User user = new User();
    if (addressTable != null) {
        while (addressTable.moveToNext()) {
            realm.executeTransaction(new Realm.Transaction(){
                @Override
                public void execute(Realm realm) {
                    User u = realm.createObject(User.class);
                    u.setPhoneNumber(addressTable.getString(addressTable.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
                    Log.d("Data", u.toString());
                }
            });
        }
        realm.close();
        addressTable.close();
    }
}
}

PhoneReceiver.java

class PhoneReceiver extends PhoneStateListener {

private Context context;
PhoneReceiver(Context context) {
    this.context = context;
}

@Override
public void onCallStateChanged(int state, String incomingNumber) {
    super.onCallStateChanged(state, incomingNumber);

    switch (state) {
        case TelephonyManager.CALL_STATE_RINGING:

            RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(context).build();
            Realm realm = Realm.getInstance(realmConfiguration);
            RealmQuery<User> results = realm.where(User.class).equalTo("phoneNumber", incomingNumber);
            int count = (int) results.count();

            if (count > 0) {
                Toast.makeText(context, "Calling" + incomingNumber, Toast.LENGTH_LONG).show();
            } else {
                Dialog();
            }
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            Toast.makeText(context, "Offhook!!" + incomingNumber, Toast.LENGTH_LONG).show();
            break;
    }
}
}

查找 PhoneUtils class 并将两个数字进行比较:

PhoneNumberUtils

compare(String a, String b)

Compare phone numbers a and b, return true if they're identical enough for caller ID purposes.