Reputation: 273
I have reviewed every post about this problem with AlertDialog to no avail. Can anyone see something amiss here? I tried DialogFragment, but am doing this from a PhoneStateListener and can't extend anything else. I do not have a null token, so getBaseContext is working. I believe.
private void lookupCallerId(int cstate)
{
if(prefs.getIsDeactivated())
return;
if(lookupInProgress)
{
return;
}
//add popup box here for lookup question?
PMLog.v(LOGTAG, "lookupCallerId() Start pop up box.");
Context context = service.getBaseContext();
if(cstate == TelephonyManager.CALL_STATE_RINGING) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
PMLog.v(LOGTAG, "lookupCallerId() ALERT BUILDER.");
builder.setTitle("Lookup this #?");
builder.setCancelable(true);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String strPhoneNumber = PhoneNumberProcessor.formattedPhoneNumber(prefs.getLastCallerNumber(), service); {
if(strPhoneNumber.length() == 0)
return;
}
PMLog.v(LOGTAG, "lookupCallerId() Starting CNM lookup thread");
Thread thread = new Thread(null, doBackgroundThreadProcessing, "LookupBackgroundThread");
thread.start();
}
}
);
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
onNo();
return;
}
});
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
onNo();
return;
}
});
builder.create().show();
}
}
Upvotes: 0
Views: 65
Reputation: 82563
The problem is due to Context context = service.getBaseContext();
UI elements can only be added through an Activity context (that is, and existing UI). Since a base context has no UI associated with it, you can't use it to add anything to the UI.
Either launch your dialog from an Activity, or use an Intent to launch a dialog themed Activity from your service.
Upvotes: 1