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();
}
});
}
}