BroadcastReceiver 是否足够异步以使其工作

发布于 2025-01-04 18:33:23 字数 1584 浏览 1 评论 0原文

想要停止线程进行一些下载。
当我只有 stopNow = true; 并且没有发生阻塞时,下面的代码工作正常。
我创建了 boolean stopNow = false; 作为 IntentService 中的一个字段。

由于 stopNow 仅当连接在 while 循环中进行时才起作用
但如果 f.ex 连接陈旧并开始阻塞,它就不起作用。

我想添加这段代码来真正阻止阻塞。

if(socket != null){
    socket.shutdownOutput();
    socket.shutdownInput();
}

问题是这是否是异步的,因此执行是否正在
while 循环 stopNow = true; 将停止它,我可以在 stopNow = true; 之后放置一个 sleep(5000),然后是 <仅当 stopNow 无效时,code>if(socket != null) 才会为 true。

希望你关注我..

位于 run() 内的 BroadcastReceiver

private class MyIncomingListener extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if(intent.getAction().equals(Consts.COM_CARLSBERG_STOPOUTGOING)) {
            String b = intent.getStringExtra(Consts.COM_CARLSBERG_BATCHUUID);
            if(b != null){
                if(b.equals(batch.batchUuid)){
                    stopNow = true;
                    // sleep(5000) wait for stopNow to take effect
                    // if socket=null then stopNow did it's job
                    // if socket is alive then there is blocking to unblock
                try{
                    if(socket != null){
                        socket.shutdownOutput();
                        socket.shutdownInput();
                    }
                } catch (Exception e) {}
               }    
            }
        }
    }
}

Wanted to stop a thread doing some downloads.
The code below work fine when i only have the stopNow = true; and no blocking occur.
I create the boolean stopNow = false; as a field in my IntentService.

Since stopNow only work when the connection is ongoing in the while loop
but it does not work if f.ex connection stales and start to block.

I wanted to add this code to really stop the blocking.

if(socket != null){
    socket.shutdownOutput();
    socket.shutdownInput();
}

The question is if this is asynchronous so if the execution is ongoing in the
while loop the stopNow = true; will stop it and i can put a sleep(5000) after the stopNow = true; and then the if(socket != null) will be true only if the stopNow had no effect.

hope you follow me..

BroadcastReceiver that are located inside the run():

private class MyIncomingListener extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if(intent.getAction().equals(Consts.COM_CARLSBERG_STOPOUTGOING)) {
            String b = intent.getStringExtra(Consts.COM_CARLSBERG_BATCHUUID);
            if(b != null){
                if(b.equals(batch.batchUuid)){
                    stopNow = true;
                    // sleep(5000) wait for stopNow to take effect
                    // if socket=null then stopNow did it's job
                    // if socket is alive then there is blocking to unblock
                try{
                    if(socket != null){
                        socket.shutdownOutput();
                        socket.shutdownInput();
                    }
                } catch (Exception e) {}
               }    
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

弥枳 2025-01-11 18:33:23

到目前为止这实际上运作良好

    stopNow = true;

try{
    if(socket != null){
        socket.shutdownOutput();
        socket.shutdownInput();
    }
} catch (Exception e) {}    

this is actually working good so far

    stopNow = true;

try{
    if(socket != null){
        socket.shutdownOutput();
        socket.shutdownInput();
    }
} catch (Exception e) {}    
捶死心动 2025-01-11 18:33:23
public final void stop ()
Since: API Level 1
This method is deprecated.
because stopping a thread in this manner is unsafe and can leave your application and the VM in an unpredictable state.

Requests the receiver Thread to stop and throw ThreadDeath. The Thread is resumed if it was suspended and awakened if it was sleeping, so that it can proceed to throw ThreadDeath.



 try {
                     int waited = 0;
                     while(_active && (waited < _splashTime)) {
                         sleep(100);
                         if(_active) {
                             waited += 100;
                         }
                     }
                 } catch(InterruptedException e) {
                     // do nothing
                 } finally {
                     finish();
                     Intent intent= new Intent(SplashActivity.this,LoginViewController.class);
                     startActivity(intent);

                     stop();
                 }
public final void stop ()
Since: API Level 1
This method is deprecated.
because stopping a thread in this manner is unsafe and can leave your application and the VM in an unpredictable state.

Requests the receiver Thread to stop and throw ThreadDeath. The Thread is resumed if it was suspended and awakened if it was sleeping, so that it can proceed to throw ThreadDeath.



 try {
                     int waited = 0;
                     while(_active && (waited < _splashTime)) {
                         sleep(100);
                         if(_active) {
                             waited += 100;
                         }
                     }
                 } catch(InterruptedException e) {
                     // do nothing
                 } finally {
                     finish();
                     Intent intent= new Intent(SplashActivity.this,LoginViewController.class);
                     startActivity(intent);

                     stop();
                 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文