So... I have found a way how to solve this issue, but it´s not the perfect way (Further details are on the bottom of this answer).
First create a Messenger:
private Messenger AlertDialogMessenger = new Messenger(new AlertDialogMessageHandler());
Second a handler, which shows up this dialog:
class AlertDialogMessageHandler extends Handler
{
@Override
public void handleMessage(Message msg)
{
super.handleMessage(msg);
AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).create();
dialog.setTitle("Attention");
dialog.setMessage((CharSequence) msg.getData().get("MESSAGE"));
dialog.setOnCancelListener(new DialogInterface.OnCancelListener()
{
@Override
public void onCancel(DialogInterface dialog)
{
//Do something
}
});
dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
//Do something
dialog.dismiss();
}
});
dialog.show();
}
}
And then you just have to send the messenger to to service:
Intent intent = new Intent(this,MyService.class);
intent.putExtra(MainActivity.EXTRA_FROM_SERVICE_MESSENGER, this.AlertDialogMessenger);
this.bindService(intent, this.conn, Context.BIND_AUTO_CREATE);
At the Service you have to catch the messenger:
@Override
public IBinder onBind(Intent intent)
{
this.AlertDialoMessenger = intent.getParcelableExtra(MainActivity.EXTRA_FROM_SERVICE_MESSENGER);
return null;
}
And then you can show up the dialog by sending a message:
Message msg = Message.obtain();
Bundle bundle = new Bundle();
bundle.putString("MESSAGE", "This is a test message");
msg.setData(bundle);
try
{
AlertDialoMessenger.send(msg);
}
catch (RemoteException e1)
{
e1.printStackTrace();
}
And that´s it.
Unfortunately I can´t use this in my case because this is only working, if you have only one Activity (In this case the MainActivity).
Because the Dialog will be only showed up in this Activity.
In my project I have more Activities and so it makes no sense to use this.
I decided to show up a short toast message like "Network error" and provide a help page in the OptionsMenu.
Thanks to Milos! and
I hope this code helps somebody sometimes :)