在我的应用程序中,我将在特定时间间隔内显示对话框,因为该时间间隔在单独的类中使用可运行方法。
我的可运行方法类
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 中一样,我不知道如何在所有活动中显示对话框。可以一个知道的请帮我解决这个问题。