标签: handler

可在 while 循环中运行

我希望我的run方法在程序运行时每 5 秒执行一次,因此我将其放入循环中while(true)。问题是我的应用程序由于这个循环而无法运行while。有人可以解释如何正确编码该循环以便它始终运行吗?

这是我的代码:

int i=0;    
while(true){
    Runnable x = new Runnable(){
        @Override
        public void run(){
            if(my_list_counter <= 0) 
                return;
            random_counter = rand.nextInt(my_list_counter);
            make_words_with_letters.add(list_of_letters.get(random_counter));
            for(Button b:button_list)
                if (b.getText().equals("")){
                    b.setText(list_of_letters.get(random_counter));
                    list_of_letters.remove(list_of_letters.get(random_counter));
                    my_list_counter--;
                    break;
                }

        }
    };
   i++;
   Handler handler = new Handler();
   //Run the code in runnable x at increasing time intervals of 5 seconds
   handler.postAtTime(x, SystemClock.uptimeMillis() + i*5000);

}
Run Code Online (Sandbox Code Playgroud)

显然这里的错误是需要Runnable到达终点才能开始运行。但是,由于它处于while(true)循环中,因此永远无法开始运行。有人知道如何解决这个问题吗?

编辑:感谢 JavaHunter 新代码,但仍然有一个错误:

int i=0;    

    Runnable x = new Runnable(){ …
Run Code Online (Sandbox Code Playgroud)

java android handler runnable

0
推荐指数
1
解决办法
4690
查看次数

Spring MVC HandlerInterceptor:重定向失败

我创建了一个简单的 WebMVC 应用程序 - 配置了处理程序拦截器。拦截器的职责很简单 - 它应该检查 HttpRequest 中是否存在有效会话 - 如果是,则重定向到注册页面。

我遇到的问题是在重定向时 - 浏览器抛出消息:

页面未正确重定向

Firefox 检测到服务器正在以永远无法完成的方式重定向对此地址的请求。

拦截器代码如下:

public class LoginInterceptor extends HandlerInterceptorAdapter{



// to be used checking session management for user.
@Override
public boolean preHandle(HttpServletRequest request,
                         HttpServletResponse response,
                         Object handler) throws Exception {


    System.out.println(" <interceptor> - this is pre handle");

    return true;
}


@Override
public void postHandle(HttpServletRequest request,
                       HttpServletResponse response, Object handler,
                       ModelAndView modelAndView) throws Exception {

    System.out.println(" <interceptor> - this is post handle");

    HttpSession session = request.getSession();
    User …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc filter handler

0
推荐指数
1
解决办法
6528
查看次数

尝试理解 Go 中的处理函数

我试图从 Golang 文档中进行此练习:https://go.dev/doc/articles/wiki/,但我不明白某些内容。在文章的第二部分中,当我们开始使用“net/http”包时,我们写了这个(我在这里留下了更完整的代码: https: //go.dev/doc/articles/wiki/part2.go):

 func viewHandler(w http.ResponseWriter, r *http.Request) {

    title := r.URL.Path[len("/view/"):]

    p, _ := loadPage(title)

    fmt.Fprintf(w, "<h1>%s</h1><div>%s</div>", p.Title, p.Body)

}


func main() {

    http.HandleFunc("/view/", viewHandler)

    log.Fatal(http.ListenAndServe(":8080", nil))

}
Run Code Online (Sandbox Code Playgroud)

我不明白为什么 viewHandler 位于 http.HandleFunc 的参数中而没有上面定义的两个参数。因为,viewHandler的定义中有两个参数:w和r?什么时候/谁完成?

http handler go

0
推荐指数
1
解决办法
111
查看次数

哪种技术最适合在 Android 中每分钟更新主屏幕应用程序小部件?

我有两种类型的主屏幕应用程序小部件 1. 由文本视图组成,2. 由列表视图组成。第一个显示来自阵列的数据,第二个从服务器提取信息(即涉及网络操作)。而且它们都需要每分钟更新一次。现在,对于每种情况,以下哪种技术最好?

a) Handler
b) Alarm Manager
c) JobScheduler
d) Firebase JobDispatcher
e) SyncAdapter
f) ..anything else if there is...
Run Code Online (Sandbox Code Playgroud)

作为 Android 初学者,很难决定哪种技术最适合设备的电池寿命。我在这里先向您的帮助表示感谢。

service android scheduling handler alarmmanager

-1
推荐指数
1
解决办法
961
查看次数

有没有办法在多个处理程序中使用相同的 request.Body 而无需手动编写大量代码,或者我需要改变我这样做的方式?

这篇很棒的文章在这里:https : //www.alexedwards.net/blog/how-to-properly-parse-a-json-request-body 很好地解释了如何编写 Golang 处理程序。

我需要使用两个处理程序,一个接一个,仅当第一个出现错误时。

像这样:

func main() {
    r := chi.NewRouter()

    r.Post("/api", MyHandlers)
}

func MyHandlers(w http.ResponseWriter, r *http.Request) {

    err := DoSomething(w, r)

    if err != nil {
        println("OMG! Error!")
        DoSomethingWithThisOneInstead(w, r)
    }

}

func DoSomething(w http.ResponseWriter, r *http.Request) error {
    // here I need to read request's Body
    // and I can use io.TeeReader()
    // and I can use all the code in the amazing article example
    // but I don't want to, because …
Run Code Online (Sandbox Code Playgroud)

architecture middleware handler go go-chi

-1
推荐指数
1
解决办法
132
查看次数

信号处理函数前3次捕获SIGKILL

我有一个编写信号处理函数的赋值,该函数捕获SIGKILL信号并在前3次调用时显示错误消息.在它第四次处理SIGKILL时,它应该将信号处理程序设置为default,然后将SIGKILL发送到它的进程(它不会捕获).

我想使用循环并在前3次迭代中显示错误消息.我对吗?我很难将SIGKILL发送到它的进程并将处理程序设置为默认值(这让我很困惑).

你能给我建议吗?

c unix signals handler sigkill

-2
推荐指数
2
解决办法
3019
查看次数

带 Looper 的 NullPointerException

请在这件事上给予我帮助。我创建了一个进度条,并在应用程序启动后每隔 100 毫秒以编程方式更新其进度(是的,这听起来很奇怪,但只是为了搞乱目的)。但每次运行它时,我都会收到 NullPointerException。有人可以帮我解决这个问题吗?日志表明 NullPointerException 发生在“custom_handler.sendMessage(message);”处 在下面。太感谢了。

private Handler custom_handler, main_handler;
private int progress = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //TextView to display the
    text_view = (TextView)findViewById(R.id.text_view); progress

    progress_bar = (ProgressBar)findViewById(R.id.progress_bar);

    //Instantiate a new worker thread
    //but somehow its handler is not instantiated
    //by the time the compiler reaches the "custom_handler.sendMessage(message);"
    //at all, keep getting NullPointerException
    //please look at the comment below the next code block.
    new MyThread().start();

    main_handler = new Handler(Looper.getMainLooper()) {
        public void handleMessage(Message …
Run Code Online (Sandbox Code Playgroud)

android nullpointerexception handler looper

-3
推荐指数
1
解决办法
1344
查看次数