着信時のみダイアログを表示したいコール受信側があります。そのために、グローバルブール変数を作成し、呼び出し状態でその値をtrueに変更しようとしました。しかし、コールが切断されたとき、コードは常に呼び出し状態で与えられた更新値ではなく、ブール値のデフォルト値を選択します。変数はnumです。なぜなら、その価値は鳴っている状態でのみ真実になりますが、それはいつも偽りの価値を与えます。 BroadcastReceiversが放送間で保存されていないので、コール受信側でブール値が更新されない
public class phonerece extends BroadcastReceiver{
private Boolean num = false;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
//some task here
}
} else if (extraState != null) {
if (extraState.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
//task
} else if (extraState
.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
if (num) {
phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
//call dialog }
}
} else if (extraState.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
if (checknumber() != null) {
Log.e("Nummber", "found");
} else {
Log.e("Number", "Not Found");
num = true;
}
}
}
}
public String checknumber() {
String res = null;
try {
ContentResolver resolver = context.getContentResolver();
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor c = resolver.query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
if (c != null) { // cursor not null means number is found contactsTable
if (c.moveToFirst()) { // so now find the contact Name
res = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
}
c.close();
}
} catch (Exception ex) {
/* Ignore */
}
return res;
}
}
あなたのsharedpreferenceアイデアはより実行可能です。 – Panache