如何在所有活动中的特定时间间隔内显示对话框?

时间:2013-05-29 05:09:31

标签: android android-handler

在我的应用程序中,我将在特定的时间间隔内显示对话框,时间间隔为am在单独的类中使用runnable方法。

我的可运行方法类

public class Timeout_function{

Handler mHandler;
Activity activity;

public Timeout_function(Activity activity,Handler mHandler) {
    super();
    this.activity = activity;
    this.mHandler = mHandler;

}

Runnable first_Task = new Runnable() {
    public void run() {


        dialog = new Dialog(activity);
        if(isShown){
            dialog.dismiss();
            dialog_msgs = "Your order is going to Expired. You want to continue.";
            dialogs();
        }
        else
        {
            dialog_msgs = "Your order is going to Expired. You want to continue.";
            dialogs();
        }

        mHandler.removeCallbacks(first_Task);
    }
    }
};

Runnable second_Task = new Runnable() {
    public void run() {





                dialog = new Dialog(activity);
                if(isShown){
                    dialog.dismiss();
                    dialog_msgs = "Your order is going to Expired. You want to continue.";
                    dialogs();
                }
                else
                {
                    dialog_msgs = "Your order is going to Expired. You want to continue.";
                    dialogs();
                }

            mHandler.removeCallbacks(second_Task);

    }
};

// just as an example, we'll start the task when the activity is started
public void onStart() {

    Time_out = 1;
    mHandler.postDelayed(first_Task, 2 * 30 * 1000);
}

public void onStart_extend(){
    mHandler.postDelayed(second_Task, 1 * 30 * 1000);
}

// at some point in your program you will probably want the handler to stop (in onStop is a good place)
public void onStop() {

        mHandler.removeCallbacks(first_Task);   
        mHandler.removeCallbacks(second_Task);

}


//Error Messages

public void dialogs(){

Typeface font = Typeface.createFromAsset(activity.getAssets(), "fonts/CALIBRI.TTF");;
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
dialog.setContentView(R.layout.dialog_timeout);
dialog.setCancelable(false);
isShown=true;

TextView tv_msg = (TextView)dialog.findViewById(R.id.dialog_texts);
tv_msg.setTypeface(font);
tv_msg.setText(""+dialog_msgs);

Button no =(Button)dialog.findViewById(R.id.dialog_no);
no.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub

        dialog.dismiss();
        order_list_remove();
        Time_out = 0;
        //onStop();
    }
});

Button yes =(Button)dialog.findViewById(R.id.dialog_yes);
yes.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub

        onStart_extend();
        dialog.dismiss();
    }
});
dialog.show();
}
}

我有活动A,B,C只会在活动b中启动这个可运行的方法,如

Timeout_function time_out ;
Handler mHandler;
mHandler = new Handler();
time_out = new Timeout_function(activityb.this, mHandler);
time_out.onStart();
这样,我希望在一定的时间间隔内显示对话框。如果用户在活动A中表示活动A中的对话框与B和c中的相同,我不知道如何在所有活动中显示对话框任何人都知道,请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

  1. 使用服务维护间隔
  2. 中显示的对话框
  3. 使用对话框活动。你可以创建它与活动相同。在清单中使用以下内容

        android:excludeFromRecents="true"
        android:launchMode="singleInstance"
        android:taskAffinity=""
        android:theme="@android:style/Theme.Dialog"
    
  4. 现在从服务开始时,间隔启动对话活动,如常规活动。