簡體   English   中英

如何從開始活動的非活動類中完成活動

[英]How to finish an activity from a non-activity class where it was started

我有一個自定義的對話框活動類,當我必須顯示特定進程的等待欄時調用該類,此后,我將其稱為完成方法,但無法在那里完成該活動,或者我不知道如何調用該活動finish方法,但是我是通過類的對象調用它的,下面是WaitDialogManager類的代碼。 我不想為此使用廣播接收器...

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()方法到達末尾時消失/消失。 謝謝

根據我的經驗,從技術上講,您無法執行此操作。 但是,您可以執行的操作是使用startActivityForResult啟動第二個活動,然后在第二個活動完成時可以傳遞回一個標志,該標志指示調用活動應退出。 該過程將是如此之快,以至於用戶將無法告知呼叫活動仍處於打開狀態。 如果第一個/調用活動應始終退出,則可以在清單中為該活動設置android:noHistory="true"

我通過從WaitDialogManager類獲取Current活動的靜態引用來解決此問題,如下所示。 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM