【问题标题】:how to show the dialog in certain interval of time in all activity?如何在所有活动中以特定时间间隔显示对话框?
【发布时间】:2013-05-29 05:09:31
【问题描述】:

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

我的可运行方法类

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 中一样,我不知道如何在所有活动中显示对话框.谁能知道请帮我解决这个问题。

【问题讨论】:

    标签: android android-handler


    【解决方案1】:
    1. 使用服务来维护按间隔显示的对话框
    2. 使用对话活动。您可以像创建活动一样创建它。在清单中使用以下内容

          android:excludeFromRecents="true"
          android:launchMode="singleInstance"
          android:taskAffinity=""
          android:theme="@android:style/Theme.Dialog"
      
    3. 现在从服务中,在间隔启动对话活动时像常规活动一样。

    【讨论】:

    • 为对话活动创建单独的类。
    • 是的,我的建议是单独的活动,因为您将在任何地方使用它
    • 如何在服务中启动activity。
    • 和普通活动一样..这只是一个带有对话框主题的普通活动..仅此而已
    • k 很好。我是服务新手,我会尝试并说。如果我有任何问题,请帮助我。
    猜你喜欢
    • 2022-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多