1

How to show AlertDialog in IntentService? Error looks like: Unable to add window -- token null is not for an application. So this is problem with context, but I don't know how to fix it. Any solutions? Below there is my code:

public class GcmMessageHandler extends IntentService {

private GoogleCloudMessaging gcm;
private static AlertDialog alert;

String mes;
private Handler handler;
public GcmMessageHandler() {
    super("GcmMessageHandler");
}

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    handler = new Handler();

}
@Override
protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();

    gcm = GoogleCloudMessaging.getInstance(this);
    String messageType = gcm.getMessageType(intent);

    mes = extras.getString("message");
    MApp.wakeupSync();

    showToast();
    Log.i("GCM", "Received : (" + messageType + ")  " + extras.getString("message"));
    GcmBroadcastReceiver.completeWakefulIntent(intent);
}

@Override
public void onDestroy() {
    try {
        gcm.unregister();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void showToast(){

    handler.post(new Runnable() {
        public void run() {
            AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
            builder.setMessage("TEST")
                    .setCancelable(false)
                    .setNegativeButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
            alert = builder.create();
            alert.show();
        }
    });

}
}
user5523912
  • 1
  • 1
  • 3

2 Answers2

1

You can only show AlertDialog using Activity context.

Here you use getApplicationContext() to create builder, but neither Application context nor Service context will work. You have to send broadcast/intent to some Activity and as a response to this message show an AlertDialog inside this Activity using Activity context.

Read this for more details: https://possiblemobile.com/2013/06/context/

mc.dev
  • 2,675
  • 3
  • 21
  • 27
  • My way is to StartNotification, send value using braodcast to intent, and show alert in the intent – user5523912 Nov 04 '15 at 16:22
  • I don't get exactly what do you mean by "show alert in the intent" The main point here is not to use getApplicationContext() it can't work. Use any Activity context instead. – mc.dev Nov 04 '15 at 16:28
  • AlertDialog in the Activity which is selected in Notification – user5523912 Nov 04 '15 at 17:10
0

Define a handler in your Activity

    /**
     * Response handler
     */
    private Handler handlerResponse = new Handler() {
        @Override
        public void handleMessage(Message msg) {
         //Show your alert here
       }
   };

Start service along with Messenger

 Intent intent = new Intent(getActivity(), CommunicationService.class);
 intent.setAction(Constants.ACTION_ONE);
 intent.putExtra(Constants.EXTRA_MESSENGER, new Messenger(handlerResponse));
 startService(intent);

In your service return the message after completing the functionality

    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
           sendResult(intent, result);
        }
     }

private void sendResult(Intent intent, String result) {
        Bundle extras = intent.getExtras();
        if (extras != null) {
            Messenger messenger = (Messenger) extras.get(Constants.EXTRA_MESSENGER);
            Message msg = Message.obtain();
            msg.obj = result;
            try {
                messenger.send(msg);
            } catch (Exception e1) {
                Log.e(getClass().getName(), "Exception sending message", e1);
            }
        }
    }

Constants

 public static final String EXTRA_MESSENGER = "EXTRA_MESSENGER";
 public static final String ACTION_ONE = "INIT_SOCKET";
Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73