0

我有一个自定义对话框活动类,当我必须显示特定进程的等待栏时调用它,之后我调用它的完成方法但我无法在那里完成该活动或者我不知道如何调用它完成方法,但我通过类的对象调用它,我的WaitDialogManager类代码如下。而且我不想为此使用广播接收器...

等待对话管理器

包 com.android.remotewipedata;

导入android.app.Activity;导入android.os.Bundle;导入android.view.Window;导入 android.widget.TextView;

公共类 WaitDialogManager 扩展 Activity {

TextView waitTitle, waitMessage;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.wait_dialog);

    String title = getIntent().getStringExtra("waitDialogTitle");
    String message = getIntent().getStringExtra("waitDialogMessage");

    waitTitle = (TextView) findViewById(R.id.wait_dialog_title);
    waitMessage = (TextView) findViewById(R.id.wait_dialog_message);
    waitTitle.setText(title);
    waitMessage.setText(message);
}

public void dismissWaitDialog(){
    this.finish();
    System.out.println("Finish Called");
}
}

这就是我调用此活动并在方法完成后尝试完成它的地方,该非活动类的代码如下

服务器实用程序

public final class ServerUtilities {
    //Other code
public static WaitDialogManager wdManager = new WaitDialogManager();

static boolean register(final Context context, String name, String email,
        final String regId) {

            // Starting WaitDialogManager activity here
    context.startActivity(new Intent(context, WaitDialogManager.class)
            .putExtra("waitDialogTitle", "Please wait...")
            .putExtra("waitDialogMessage",
                    "Registering device on Server...")
            .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));

    String serverUrl = SERVER_URL + "/register.php";
    Map<String, String> params = new HashMap<String, String>();
    params.put("regId", regId);
    params.put("name", name);
    params.put("email", email);

    // Try to register on server for a number of times
    long backoff = BACKOFF_MILLI_SECONDS + random.nextInt(1000);
    for (int i = 1; i <= MAX_ATTEMPTS; i++) {
        try {
            post(serverUrl, params);
            System.out.println("Parameters: " + params);
            GCMRegistrar.setRegisteredOnServer(context, true);
            return true;
        } catch (IOException e) {
            if (i == MAX_ATTEMPTS) {
                break;
            }
            try {
                Thread.sleep(backoff);
            } catch (InterruptedException e1) {
                Thread.currentThread().interrupt();
                return false;
            }
            backoff *= 2;
        }
    }
    wdManager.dismissWaitDialog();
    return false;
}

为了使此对话框消失,我手动单击后退按钮使其消失,我希望它在register()方法结束时关闭/消失。谢谢

4

2 回答 2

0

根据我的经验,您在技术上无法做到这一点。不过,您可以做的是启动第二个活动,startActivityForResult然后当您的第二个活动完成时,您可以传回一个指示调用活动应该退出的标志。这个过程将非常快,以至于用户将无法判断调用活动是否仍处于打开状态。如果第一个/调用活动应始终退出,那么您可以在清单中android:noHistory="true"为活动设置。

于 2013-06-14T07:41:37.857 回答
0

我通过从类中获取当前活动的静态引用来解决它,WaitDialogManager如下所示。在WaitDialogManager类中声明静态Activity引用,

public class WaitDialogManager extends Activity {

public static Activity context = null;   // Current Activity

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.wait_dialog);
            ....
}
}

ServerUtilities像打击一样呼唤它,

public final class ServerUtilities {
//Other code

static boolean register(final Context context, String name, String email,
    final String regId) {

        // Starting WaitDialogManager activity here
         context.startActivity(new Intent(context, WaitDialogManager.class)
        .putExtra("waitDialogTitle", "Please wait...")
        .putExtra("waitDialogMessage",
                "Registering device on Server...")
        .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));

String serverUrl = SERVER_URL + "/register.php";
Map<String, String> params = new HashMap<String, String>();
params.put("regId", regId);
params.put("name", name);
params.put("email", email);

// Try to register on server for a number of times
long backoff = BACKOFF_MILLI_SECONDS + random.nextInt(1000);
for (int i = 1; i <= MAX_ATTEMPTS; i++) {
    try {
        post(serverUrl, params);
        System.out.println("Parameters: " + params);
        GCMRegistrar.setRegisteredOnServer(context, true);
        WaitDialogManager.context.finish(); // And this is how it works
        return true;
    } catch (IOException e) {
        // Exception handled
    }
}
return false;
}
于 2013-06-17T06:40:42.067 回答