Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:如何为BroadcastReceiver onReceive方法等待IntentService_Android_Wait_Intentservice_Notify_Android Broadcastreceiver - Fatal编程技术网

Android:如何为BroadcastReceiver onReceive方法等待IntentService

Android:如何为BroadcastReceiver onReceive方法等待IntentService,android,wait,intentservice,notify,android-broadcastreceiver,Android,Wait,Intentservice,Notify,Android Broadcastreceiver,我的IntentService需要在方法a()中等待BroadcastReceiver()的onReceive()结果 现在我使用lmao等待(5000)。。。所以它不太优雅 IntentService: private boolean methodA() { try { synchronized (mLocalBroadcastReceiver) { mLocalBroadcastReceiver.wait(3000);

我的IntentService需要在方法a()中等待BroadcastReceiver()的onReceive()结果

现在我使用lmao等待(5000)。。。所以它不太优雅

IntentService:

private boolean methodA() {

try {
            synchronized (mLocalBroadcastReceiver) {
                mLocalBroadcastReceiver.wait(3000); 
            }
        } catch (InterruptedException e) {
            Log.e(TAG,"error, thread interrupted");
            e.printStackTrace();
        }

if(CONSTANT == true){ 
   return true;
else 
   return false;
}
@Override
public void onReceive(Context context, Intent intent) {
    CONSTANT = true //changes somehow between true/false 
}
广播接收器:

private boolean methodA() {

try {
            synchronized (mLocalBroadcastReceiver) {
                mLocalBroadcastReceiver.wait(3000); 
            }
        } catch (InterruptedException e) {
            Log.e(TAG,"error, thread interrupted");
            e.printStackTrace();
        }

if(CONSTANT == true){ 
   return true;
else 
   return false;
}
@Override
public void onReceive(Context context, Intent intent) {
    CONSTANT = true //changes somehow between true/false 
}

换句话说:methodA的返回值取决于onReceive()的结果。如何同步两个线程?

最后我使用了如下线程:

 private void waitForResponse() {
        //wait for response
        thread = new WaitForStatus();
        thread.run();
        try {
            synchronized (thread) {
                thread.wait();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private class WaitForAppStatus implements Runnable {
            @Override
            public void run() {
                while (true) {
                    if (CONSTANT  != -1) {
                        break;
                    } else {
                        try {
                            wait(400);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }

最后我使用了这样的线程:

 private void waitForResponse() {
        //wait for response
        thread = new WaitForStatus();
        thread.run();
        try {
            synchronized (thread) {
                thread.wait();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private class WaitForAppStatus implements Runnable {
            @Override
            public void run() {
                while (true) {
                    if (CONSTANT  != -1) {
                        break;
                    } else {
                        try {
                            wait(400);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }

在onReceive methodstart中调用methodA(),方法是阅读IntentService的文档,因为它不是这样的for@TimCastelijns我的IntentService执行更多其他卸载任务,而不仅仅是methodA(),methodA()是onReceive Method内部的一个小部件调用methodA(),请阅读IntentService的文档开始,因为事实并非如此for@TimCastelijns我的IntentService做更多的其他卸载任务,而不仅仅是methodA(),methodA()只是其中的一小部分