如何从启动它的非活动类中完成活动

时间:2013-06-14 06:46:27

标签: android android-activity alertdialog

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

WaitDialogManager

包com.android.remotewipedata;

import android.app.Activity; import android.os.Bundle; import android.view.Window; import 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");
}
}

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

之下

ServerUtilities

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()方法到达时结束时消失/消失。感谢

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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;
}