English 中文(简体)
如何在任何关于 Java安的任务之前开展活动?
原标题:How to Run Event before any task on Java Android?

我已经制定法典,并努力实施这一法典:

class Example extends Dialog {
    @Override
    public void dismiss() {
        super.dismiss();
        if (isForceExists != false) {
            int id = android.os.Process.myPid();
            android.os.Process.killProcess(id);
        }
    }
}

但是,当我想在离开申请之前举行其他活动,而不是工作(小规模活动)。

class ExampleActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
       Example exDialog = new Example();
       exDialog.setDismissListener(new OnDismissListener() {
           AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
           builder.setTitle("Exit Application")
                 .setMessage("Thank you for using this application, happy nice day ^_^")
                 .show();
       });
    }
}

我想在申请退出之前放弃或等待进展,怎么做? 但是,在没有发生事前,这只是强迫离开?

====================================

谢谢你的建议。 首先,我要对我的问题缺乏解释表示歉意。 因此,我的详细问题是如何警惕。 在完成(a)行动之前建立起来?

我想首先搁置警示,或处理这一事件。 此外,还击退了Example Dialog。

@Override
public void onDismiss(DialogInterface dialog) {
    // --> I want to hold AlertDialog first, or handling this event.
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Exit Application")
            .setMessage("Thank you for using this application, happy nice day ^_^")
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface subDialog, int which) {
                    // --> And After show AlertDialog, back event to dismiss 
                    //     of Example extends Dialog again.
                    ExampleDialog.dismiss();
                }
            }).show();
}

Running AlertDialog, before finishing. And then return back to dismiss and exit application without from Activity class, but exit or finish from method s Example Dialog class.

class Example extends Dialog {
    public boolean isForceExists = true;
    @Override
    public void dismiss() {
        super.dismiss();
        if (isForceExists != false) {
            int id = android.os.Process.myPid();
            android.os.Process.killProcess(id);
        }
    }
}
问题回答

您应处理贵方特类或独类行为本身的事件逻辑,而不是试图在显示方言时,在例常识方法范围内进行活动。

Modify your Example class to include a callback for the dismissal event:

class Example extends Dialog {
    private OnDismissListener dismissListener;

    public void setDismissListener(OnDismissListener listener) {
        this.dismissListener = listener;
    }

    @Override
    public void dismiss() {
        if (dismissListener != null) {
            dismissListener.onDismiss(this);
        }
        super.dismiss();
    }
}

Modify your ExampleActivity to implement the OnDismissListener interface and override its onDismiss method to execute your desired event before exiting the application:

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;

public class ExampleActivity extends Activity implements DialogInterface.OnDismissListener {

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

        Example exDialog = new Example(this);
        exDialog.setDismissListener(this);
        exDialog.show();
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        // This method will be called before the dialog is dismissed.
        // You can perform your desired event here.
        // For example, showing a Toast message.
        Toast.makeText(this, "Before exit event executed.", Toast.LENGTH_SHORT).show();
        
        // After the event, you can exit the application if needed.
        finish();
    }
}

这种做法确保,在申请之前,当你辞退方言时,你的活动才会停止。





相关问题
Mysql trigger/events vs Cronjob

I have an auction website which let my users place an unlimited amount of autobiddings. To monitor these autobiddings something has to check the database every second. My question is if it is ...

Can an event be used as an event listener?

I m trying to expose some events from a private object which is contained inside the object I am creating and it looks like the compiler is happy with this: private WindowUpdateServer ...

鸡奸

由于将javascript DOM方法放在第html页底部(在<有人>之后)远比利用“j Query”准备活动要快得多,因此我们不得不这样做:

Attaching a property to an event in Flex/AS3

I have a parameter that needs to be passed along with an event. After unsuccessful attempts to place it on the type by extending the class, I ve been advised in another SO question to write a custom ...

jQuery bind on ajax load() event

I have a page which displays multiple blocks with results details. Inside each block I have some <a> tags with thickbox jQuery plugin attached: class="thickbox" Here is an example of one kind ...

HTML text input event

I have a form, and want to disable/enable its submit button depending on whether the form s input text fields are empty or have had text entered into them. I think this means having an event handler ...

IE doesn t run Javascript when you click back button

I ve built a shop with tons of JS running. One of the pieces of the cart, is a zip code field which is required to calculate shipping cost. This works fine going through the cart. Now clicking ...

热门标签