如果活动正在运行,则禁止来自服务的通知
我有一个活动和服务在我的应用程序中协同工作。我已将该服务配置为远程服务(实现了 AIDL),因此即使活动不可见,它也会继续运行。
该服务负责轮询服务器以获取数据,并在满足某些条件时发送警报通知。我不希望服务在活动可见时发送这些通知。
服务是否有办法了解任何特定活动的状态?特别是与其绑定的活动?
已更新清单以解决权限问题:
<?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>
我在 Logcat 中遇到的错误:
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)
发送广播的任务:
@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);
}
}
}
I have an Activity and Service that work together in my application. I've configured the service as a remote service (implemented AIDL) so it will keep running even when the Activity isn't visible.
The service is responsible for polling a server for data and sending alert notifications when certain criteria are met. I do not want the Service to send these notifications when the Activity is visible.
Is there a way for the Service to know the status of any particular activity? Particularly an activity that is bound to it?
updated with manifest to troubleshoot permission problem:
<?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'm getting in Logcat:
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)
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);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以实现某种回调系统。
但有一个更干净的方法来解决这个问题。
使用有序广播。让活动有一个高优先级接收者;将引发通知逻辑置于低优先级接收器中。让 Activity 的接收者调用使用事件总线:greenrobot 的 EventBus、abortBroadcast()
以阻止通知。我有一篇关于LocalBroadcastManager
、基于 Rx 的事件总线,甚至是MutableLiveData
。让服务人员向公交车发布消息。当 UI 位于前台时,让 UI 层注册总线上的事件。如果没有人接收放置在总线上的事件,则让服务发出通知
。You can implement some sort of callback system.
But there's a cleaner way to solve the problem.
Use an ordered broadcast. Have the activity have a high-priority receiver; have the raise-a-notification logic be in a low-priority receiver. Have the activity's receiver callUse an event bus: greenrobot's EventBus,abortBroadcast()
to prevent the notification. I have a blog post up about the technique.LocalBroadcastManager
, an Rx-based event bus, or even aMutableLiveData
. Have the service post a message to the bus. Have the UI layer register for events on the bus when the UI is in the foreground. Have the service raise aNotification
if nobody picks up the event placed onto the bus.