我有一个在服务中注册的动态广播接收器,我的服务正在一段时间(某些条件)循环中进行一些重的SD卡读/写操作。
当我的另一个应用程序(在其他过程中)发送广播时,我的广播接收器没有收到广播。
当循环没有执行时,接收到相同的广播。
我还尝试将Thread.Sleep(100)的循环结束,只是为了给广播接收器一些时间来执行但是它不起作用。
任何有关这方面的帮助都会对我有所帮助。
- 感谢和问候,Manju
Code below for registering BxRx: this.registerReceiver(myReceiver, new IntentFilter(ACTIVITY_NAME)); code below for sending broadcast: Intent intnt = new Intent(ACTIVITY_NAME); intnt.putExtra("STOP_ALL_TESTING", true); Log.d(TAG,"Sending BX STOP_ALL_TESTING"); myActivity.this.sendBroadcast(intnt); code below for while loop: while(somecondition){ : : : Thred.sleep(100); } public void onReceive(Context context, Intent intent) { Log.d(TAG,"Received intent: "+intent.getAction()); boolean flag = intent.getBooleanExtra("STOP_ALL_TESTING", false); Log.d(TAG,"Flag set to: "+flag); if((boolean)intent.getBooleanExtra("STOP_ALL_TESTING",false)){ Log.d(TAG,"Broadcast received to STOP_ALL_TESTING"); Log.d(TAG,"Bx Rx, setting flag to stop testing as requested by user"); synchronized(this){ bStopTesting=true; } } }I have a dynamic broadcast receiver registered in a service and my service is doing some heavy sdcard read/write operation in a while(somecondition) loop.
When a broadcast is sent from my another Application (which is in other process) is not received by my broadcast receiver.
This same broadcast is received when it is not executing while loop.
I also tried to put end of loop with Thread.Sleep(100) just to give some time for broadcast receiver to get executed but it is not working.
Any help regarding this will help me a lot.
-Thanks & regards, Manju
Code below for registering BxRx: this.registerReceiver(myReceiver, new IntentFilter(ACTIVITY_NAME)); code below for sending broadcast: Intent intnt = new Intent(ACTIVITY_NAME); intnt.putExtra("STOP_ALL_TESTING", true); Log.d(TAG,"Sending BX STOP_ALL_TESTING"); myActivity.this.sendBroadcast(intnt); code below for while loop: while(somecondition){ : : : Thred.sleep(100); } public void onReceive(Context context, Intent intent) { Log.d(TAG,"Received intent: "+intent.getAction()); boolean flag = intent.getBooleanExtra("STOP_ALL_TESTING", false); Log.d(TAG,"Flag set to: "+flag); if((boolean)intent.getBooleanExtra("STOP_ALL_TESTING",false)){ Log.d(TAG,"Broadcast received to STOP_ALL_TESTING"); Log.d(TAG,"Bx Rx, setting flag to stop testing as requested by user"); synchronized(this){ bStopTesting=true; } } }最满意答案
请粘贴完整的代码。
看起来你的问题是你在服务的onStartCommand方法中有一个无限循环。 onStartCommand和onReceive都在同一个线程上执行,并且只能一个接一个地执行。 应用程序主线程是一个Looper线程,它以顺序方式处理事件。 基本上,如果您在服务中有无尽的操作,您将阻止整个主线程,其中包括所有GUI,服务和广播接收器。 调用Thread.sleep()无济于事,因为该方法不会返回。 为避免这种情况,您可以使用IntentService http://developer.android.com/reference/android/app/IntentService.htmlclass ,它将处理另一个线程上的意图。
public class HeavyService extends IntentService { public HeavyService() { super("HeavyService"); } @Override public void onCreate() { super.onCreate(); //do your initialization } @Override protected void onHandleIntent(Intent intent) { //this will be executed on a separate thread. Put your heavy load here. This is //similar to onStartCommand of a normal service } }Please paste your complete code.
It looks like your problem is that you have an endless loop in service's onStartCommand method. Both onStartCommand and onReceive are executed on the same thread and only one after another. Applications main thread is a Looper thread, which handles events in a sequential manner. Basically, if you have an endless operation in the service, you will block the whole main thread, which includes all the GUI, services and Broadcast receivers. Calling Thread.sleep() won't help, because the method does not return. To avoid this, you can use IntentService http://developer.android.com/reference/android/app/IntentService.htmlclass, which will handle intents on another thread.
public class HeavyService extends IntentService { public HeavyService() { super("HeavyService"); } @Override public void onCreate() { super.onCreate(); //do your initialization } @Override protected void onHandleIntent(Intent intent) { //this will be executed on a separate thread. Put your heavy load here. This is //similar to onStartCommand of a normal service } }更多推荐
发布评论