I am writing an android app to give the user the option to call a different alternative number based on the number he tried to call. To do that I have BroadCastReceiver that gets the number being called, checks if there are alternatives and then to show the dialog it starts a new activity to do that as it can not do on its own. Everything is ok except the activity get closed before the dialog appears, so I get a window leaked exception but I can not see how I can make it works, waiting for the result of the dialog (uncomment the loop in the NumberDialog class) results in no exception but no dialog.
The sources are:
package com.frisco.hello;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.SQLException;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;
import com.frisco.hello.util.DataBaseHelper;
public class Caller extends BroadcastReceiver {
private static final String TAG = "HelloWorld";
@Override
public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
String number = getResultData();
if (intentAction.equals(Intent.ACTION_NEW_OUTGOING_CALL) && number != null)
{
Log.d(TAG, "Recibido evento por el numero" + number);
DataBaseHelper myDbHelper = new DataBaseHelper(context);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
}catch(SQLException sqle){
throw sqle;
}
String[] results = myDbHelper.getAlternateNumbers(number);
if (results.length > 1) {
intent = new Intent(Intent.ACTION_CALL,
Uri.fromParts("com.frisco.hello.number", Uri.decode(number),null));
number = null;
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
}
}
Number dialog:
package com.frisco.hello;
import java.io.IOException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.database.SQLException;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import com.frisco.hello.util.DataBaseHelper;
public class NumberDialog extends Activity {
private static final String TAG = "HelloWorld";
private CharSequence[] alternates;
private CharSequence selected = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Uri uri = getIntent().getData();
String target = uri.getSchemeSpecificPart();
Log.d(TAG,"Decodificado el numero como "+target);
DataBaseHelper myDbHelper = new DataBaseHelper(this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
}catch(SQLException sqle){
throw sqle;
}
String[] results = myDbHelper.getAlternateNumbers(target);
alternates = results;
new AlertDialog.Builder( this)
.setTitle( "Select number" )
.setItems( results, new DialogSelectionClickHandler())
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
finish();
}
})
.setOnCancelListener(new OnCancelListener() {
public void onCancel(DialogInterface dialog) {
finish();
}
})
.show();
/* while (selected == null) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
;
}
}
Log.i( TAG, "Seleccionado " + selected );
*/
}
public class DialogSelectionClickHandler implements DialogInterface.OnClickListener, DialogInterface.OnDismissListener
{
@Override
public void onClick(DialogInterface dialog, int which) {
Log.i( TAG, which+"_"+alternates[which] );
selected = alternates[which];
}
@Override
public void onDismiss(DialogInterface dialog) {
Log.i( TAG, "Dismissed" );
selected = alternates[0];
}
}
}
LogCat
09-29 17:52:28.309: ERROR/WindowManager(4464): Activity com.frisco.hello.NumberDialog has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@43e48270 that was originally added here
09-29 17:52:28.309: ERROR/WindowManager(4464): android.view.WindowLeaked: Activity com.frisco.hello.NumberDialog has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@43e48270 that was originally added here
09-29 17:52:28.309: ERROR/WindowManager(4464): at android.view.ViewRoot.<init>(ViewRoot.java:247)
09-29 17:52:28.309: ERROR/WindowManager(4464): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
09-29 17:52:28.309: ERROR/WindowManager(4464): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
09-29 17:52:28.309: ERROR/WindowManager(4464): at android.view.Window$LocalWindowManager.addView(Window.java:424)
09-29 17:52:28.309: ERROR/WindowManager(4464): at android.app.Dialog.show(Dialog.java:241)
09-29 17:52:28.309: ERROR/WindowManager(4464): at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
09-29 17:52:28.309: ERROR/WindowManager(4464): at com.frisco.hello.NumberDialog.onCreate(NumberDialog.java:56)
09-29 17:52:28.309: ERROR/WindowManager(4464): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-29 17:52:28.309: ERROR/WindowManager(4464): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-29 17:52:28.309: ERROR/WindowManager(4464): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-29 17:52:28.309: ERROR/WindowManager(4464): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-29 17:52:28.309: ERROR/WindowManager(4464): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-29 17:52:28.309: ERROR/WindowManager(4464): at android.os.Handler.dispatchMessage(Handler.java:99)
09-29 17:52:28.309: ERROR/WindowManager(4464): at android.os.Looper.loop(Looper.java:123)
09-29 17:52:28.309: ERROR/WindowManager(4464): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-29 17:52:28.309: ERROR/WindowManager(4464): at java.lang.reflect.Method.invokeNative(Native Method)
09-29 17:52:28.309: ERROR/WindowManager(4464): at java.lang.reflect.Method.invoke(Method.java:521)
09-29 17:52:28.309: ERROR/WindowManager(4464): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-29 17:52:28.309: ERROR/WindowManager(4464): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-29 17:52:28.309: ERROR/WindowManager(4464): at dalvik.system.NativeStart.main(Native Method)