Alex VII
Alex VII

Reputation: 930

How can I build a dialog which is launched by a service?

I think my problem is easy to fix for you. I have a service running in the background and starting an Intent which starts an activity, which opens a dialog. Her is my code from the service:

Intent todialog = new Intent();
todialog.setClass(myService.this, openDialogInSleep.class);
todialog.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(todialog);

and here is the activity:

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;

public class openDialogInSleep extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    new AlertDialog.Builder(openDialogInSleep.this)
    .setTitle("huhu")
    .setNeutralButton("close", null);
}   
}

Finally the android manifest:

<activity android:name=".openDialogInSleep" android:theme="@android:style/Theme.Dialog"></activity>

My Problem is, that there is not shown the dialog with the title "huhu" and the button "close". There is only shown a dialog in a strange form which simply shows a part of my activityname. What did I forget? Please help me.

mfg. Alex

Upvotes: 2

Views: 107

Answers (2)

rogermushroom
rogermushroom

Reputation: 5586

One thing you have missed is to call show() on the dialog generated by the AlertBuilder.

UPDATE

If I understand you correctly you don't want the activity to take control of the screen but rather just show the dialog?

If that is the case I don't think it is possible, the only way you can create a similar effect is by using a Toast Message. This can be launched from your Service and you wouldn't require the activity at all.

Upvotes: 1

user658042
user658042

Reputation:

You forget to chain in show() to actually show your dialog.

new AlertDialog.Builder(openDialogInSleep.this)
    .setTitle("huhu")
    .setNeutralButton("close", new OnClickListener() {

        @Override
        public void onClick(DialogInterface arg0, int arg1) {
            openDialogInSleep.this.finish();
        }

    })
    .show();

Upvotes: 2

Related Questions