Android 如果活动正在运行,则禁止来自服务的通知
在我的应用程序中,我有一个活动和服务可以协同工作。我已经将该服务配置为远程服务(实现了AIDL),因此即使活动不可见,它也会继续运行 该服务负责轮询服务器以获取数据,并在满足某些条件时发送警报通知。我不希望服务在活动可见时发送这些通知 服务是否有办法了解任何特定活动的状态?尤其是与之相关的活动? 已使用清单更新以解决权限问题:Android 如果活动正在运行,则禁止来自服务的通知,android,android-activity,android-service,Android,Android Activity,Android Service,在我的应用程序中,我有一个活动和服务可以协同工作。我已经将该服务配置为远程服务(实现了AIDL),因此即使活动不可见,它也会继续运行 该服务负责轮询服务器以获取数据,并在满足某些条件时发送警报通知。我不希望服务在活动可见时发送这些通知 服务是否有办法了解任何特定活动的状态?尤其是与之相关的活动? 已使用清单更新以解决权限问题: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schem
<?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>
发送广播的任务:
@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);
}
}
}
服务是否有办法了解任何特定活动的状态?特别是一项
这是必然的吗
您可以实现某种回调系统
但是有一个更干净的方法来解决这个问题。使用有序的广播。使活动具有高优先级接收器;将raise-a-notification逻辑置于低优先级接收器中。让活动的接收器调用
abortBroadcast()
来阻止通知。我对这项技术有点怀疑。使用事件总线:greenrobot的事件总线、LocalBroadcastManager
、基于Rx的事件总线,甚至是MutableLiveData
。让服务人员在公共汽车上留言。当UI位于前台时,让UI层注册总线上的事件。如果没有人接车上的事件,请让服务发出通知。正如您在博客文章中提到的,这非常适合我尝试做的事情。我曾经遇到过一个问题,我认为这一切都会奏效。我被告知,拒绝许可基本上是阻止我的活动和广播接收器接收到意图。我在清单中只使用了一个权限(INTERNET)(用它更新了问题)。我为什么会得到这个?@twilbrand:我不知道。你是如何发送广播的?从我的远程服务。我有一个每30秒运行一次的任务。我已经在问题中添加了代码。@commonware:太棒了,非常感谢。很好用@H4F:对不起,我不知道你指的是什么。我建议您在这个主题上提出一个单独的堆栈溢出问题。
@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);
}
}
}