Disable notifications from the service if activity is running

I have Activity and Service that work together in my application. I configured the service as a remote service (implemented by AIDL) so that it continues to work even if the activity is not visible.

The service is responsible for polling the server for data and sending alerts about alerts when certain criteria are met. I do not want the Service to send these notifications when activity is visible.

Is there a way for the Service to know the status of any particular activity? In particular, is it connected with her?

updated by manifest to resolve resolution issue:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.interact.listen.android.voicemail" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MyAppName" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MyObjectDetails"/> <service android:enabled="true" android:name=".MyService" /> <receiver android:name=".MyReceiver"> <intent-filter> <action android:name="com.foo.bar.EVENT"/> </intent-filter> </receiver> </application> <uses-permission android:name="android.permission.INTERNET"></uses-permission> </manifest> 

The error I get in Logcat is:

 09-17 15:33:17.881: WARN/ActivityManager(53): Permission Denial: receiving Intent { act=com.foo.bar.myobject.EVENT (has extras) } to ProcessRecord{43928b40 223:com.foo.bar.myobject/10022} (pid=223, uid=10022) requires com.foo.bar.myobject.EVENT due to sender com.foo.bar.myobject (uid 10022) 09-17 15:33:48.901: WARN/ActivityManager(53): Permission Denial: receiving Intent { act=com.foo.bar.myobject.EVENT (has extras) } to com.foo.bar.myobject requires com.foo.bar.myobject.EVENT due to sender com.foo.bar.myobject (uid 10022) 

The task that sends the broadcast:

 @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); serviceHandler = new Handler(); serviceHandler.postDelayed(myTask, 100L); Log.d(TAG, "onStart()"); } class Task implements Runnable { public void run() { Log.i(TAG, "Getting myObjects..."); getMyObjects(); Bundle bundle = new Bundle(); bundle.putLongArray("ids", getIdsToUpdate()); Intent i = new Intent(); i.setAction(UPDATE_ACTION_STRING); i.putExtras(bundle); sendOrderedBroadcast(i, UPDATE_ACTION_STRING); serviceHandler.postDelayed(this, 30000L); } } } 
+6
android android-activity android-service
source share
1 answer

Is there a way for the Service to know the status of any particular activity? In particular, an activity that is attached to it?

You can implement some callback system.

But there is a cleaner way to solve the problem. Use streamlined streaming. This event has a high priority receiver; There is a logic for raising notification in a low priority receiver. Ask the activity receiver to call abortBroadcast() to prevent notification. I have a blog post about technology. Use the event bus: greenrobot EventBus, LocalBroadcastManager , Rx-based, or even MutableLiveData . Send a message to the service on the bus. The user interface register for events on the bus when the user interface is in the foreground. Ask the service to raise the Notification if no one picks up an event posted on the bus.

+8
source share

All Articles