Android解决回调地狱的好办法!!

792 阅读1分钟
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    final Object lock = new Object();
    boolean is = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //这段代码非常有用,一定要好好保留(子线程用同步的方式等待主线程弹窗选择的结果后在继续执行,完全用同步的方式实现一般用异步才能实现的操作)
        new Thread(() -> {
            Log.e("","抉择前");
            runOnUiThread(() -> {
                boolean result = new BlockingChoiceDialog(MainActivity.this,"true or false").showDialog(); //这里用阻塞式弹窗
                if (result){
                    Log.e("","选择了true");
                    is = true;
                }
                else {
                    Log.e("","选择了false");
                    is = false;
                }
                synchronized (lock) {
                    lock.notifyAll();
                }
            });
            synchronized (lock) {
                try {
                    lock.wait();
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            Log.e("","这里执行到了?is = " + is);
        }).start();
    }

}

这种弹窗是阻塞式弹窗,可以直接在show后返回操作结果,主线程会阻塞住,但是不会发生anr

import android.app.Activity;
import android.app.Dialog;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 动态询问用户的意见后再执行后面的流程
 * 这是一个模态阻塞对话框(阻塞主线程,结果不用回调来处理)
 */
public class BlockingChoiceDialog extends Dialog {


    boolean dialogResult;
    Handler mHandler;
    Activity context;
    String question;

    public BlockingChoiceDialog(Activity context, String question) {
        super(context);
        setOwnerActivity(context);
        this.context = context;
        this.question = question;
        onCreate();
    }

    public boolean getDialogResult() {
        return dialogResult;
    }

    public void setDialogResult(boolean dialogResult) {
        this.dialogResult = dialogResult;
    }

    /**
     * Called when the activity is first created.
     */

    public void onCreate() {
        LinearLayout contentView = new LinearLayout(context);
        contentView.setOrientation(LinearLayout.HORIZONTAL);
        setContentView(contentView);
        TextView title = new TextView(context);
        title.setText(question);
        Button yesButton = new Button(context);
        yesButton.setText("YES");
        yesButton.setOnClickListener(v -> endDialog(true));
        Button noButton = new Button(context);
        noButton.setText("NO");
        noButton.setOnClickListener(v -> endDialog(false));
        contentView.addView(title);
        contentView.addView(yesButton);
        contentView.addView(noButton);
    }

    public void endDialog(boolean result) {
        dismiss();
        setDialogResult(result);
        Message m = mHandler.obtainMessage();
        mHandler.sendMessage(m);
    }

    public boolean showDialog() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message mesg) {
                // process incoming messages here
                //super.handleMessage(msg);
                throw new RuntimeException();
            }
        };
        super.show();
        try {
            Looper.getMainLooper().loop();
        } catch (RuntimeException e2) {
        }
        return dialogResult;
    }

}

任何代码都不是万能的,好用不能滥用,要看使用的时机,有适合回调也确实是更好的方式。