Archive

Posts Tagged ‘specific’

Auto-close dialog after a specific time

July 13, 2011 11 comments

Not like Toast which is auto closed after 1-2 seconds, Dialog by default is not auto closed and doesn’t have any settings for auto closing.

In case you want your Dialog to auto-close after a time, then you may use a Timer to handle this task. Here a sample:

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;

public class AlertDialogStudy extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // get button
        Button btnShow = (Button)findViewById(R.id.btn_show);
        btnShow.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
                builder.setTitle("Auto-closing Dialog");
                builder.setMessage("After 2 second, this dialog will be closed automatically!");
                builder.setCancelable(true);

                final AlertDialog dlg = builder.create();

                dlg.show();

                final Timer t = new Timer();
                t.schedule(new TimerTask() {
                    public void run() {
                        dlg.dismiss(); // when the task active then close the dialog
                        t.cancel(); // also just top the timer thread, otherwise, you may receive a crash report
                    }
                }, 2000); // after 2 second (or 2000 miliseconds), the task will be active.

            }
        });
    }
}

I guess it’s easy enough for you to understand, right?

Hope you learn something from it!

 

Cheers,

Pete Houston

Advertisements

Connect to a specific device in Monkey script

July 7, 2011 1 comment

There are cases when you run many emulators in order to test their functions like sending messages, calling to each others …

and in Monkey scripts you need to connect to specific one.

Look at documentation on MonkeyRunner, you’ll see how to connect to a specific device:

Sample: if I want to connect to a device: “emulator-5556”, my connection part of script would be like:

device = MonkeyRunner.waitForConnection(120, "emulator-5556")

That’s all for it, now you can handle the rest of automation test for device.

Cheers,
Pete Houston