开发者

Dialog box in Timer task

I am starting the Timertask in Oncreate of an Activity. The timer task listens to the mic input and determines the the sound frequency. whenever the frequency is above certain range, i need to display the Dialogbox with the message. The following code looks good to me, but the DIalog box does not popup. Any suggestion would be appreciated

    Context mContext = getApplicationContext();
    System.err.println("Inside Dialog");
    Dialog dialog = new Dialog开发者_运维问答(mContext);
    dialog.setContentView(R.layout.entryofferdialog);
    dialog.setTitle("This is my custom dialog box");
    dialog.setCancelable(true);
    // set up text
    TextView text = (TextView) dialog.findViewById(R.id.TextView01);
    text.setText(":LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL");

    // set up button
    Button button = (Button) dialog.findViewById(R.id.Button01);
    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
    // now that the dialog is set up, it's time to show it
    dialog.show();
}


I used to Handlers to get it resolved.


I used a Handler like this.

    public class Lessons extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.lessons);

    Handler handler = new Handler();

    handler.postDelayed(new Runnable() {
        public void run() {
            Dialog dialogLessons = new Dialog(Lessons.this);
            dialogLessons.setContentView(R.layout.test_dialog_box);
            dialogLessons.setTitle("Lesson test dialog title");
            dialogLessons.setCancelable(false);

            TextView text = (TextView)
                    dialogLessons.findViewById(R.id.textView1);
            text.setText(R.string.lots_of_text);

            dialogLessons.show();

        }
    }, 2000);
}

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜