【问题标题】:Suppress notifications from a service if activity is running如果活动正在运行,则禁止来自服务的通知
【发布时间】:2010-09-16 22:16:27
【问题描述】:

我有一个 Activity 和 Service 在我的应用程序中协同工作。我已将该服务配置为远程服务(已实现 AIDL),因此即使 Activity 不可见,它也会继续运行。

该服务负责轮询服务器以获取数据并在满足某些条件时发送警报通知。我不希望服务在活动可见时发送这些通知。

服务有没有办法知道任何特定活动的状态?特别是绑定到它的活动?

更新了清单以解决权限问题:

<?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);
        }
    }
}

【问题讨论】:

    标签: android android-activity android-service


    【解决方案1】:

    服务有没有办法知道任何特定活动的状态?特别是一项活动 是绑定的吗?

    你可以实现某种回调系统。

    但是有一种更简洁的方法可以解决问题。 使用有序广播。让活动有一个高优先级的接收者;让 raise-a-notification 逻辑位于低优先级接收器中。让活动的接收者致电abortBroadcast() 以阻止通知。我有一个关于该技术的blog post 使用事件总线:greenrobot 的 EventBus、LocalBroadcastManager、基于 Rx 的事件总线,甚至是 MutableLiveData。让服务人员向总线发布消息。当 UI 在前台时,让 UI 层注册总线上的事件。如果没有人接收放在公共汽车上的事件,请让服务提出Notification

    【讨论】:

    • 正如您在博客文章中提到的那样,这非常适合我正在尝试做的事情。在我认为这一切都会奏效之前,我遇到了一个问题。我被告知,拒绝许可基本上会阻止我的活动和我的广播接收器接收意图。我在清单中只使用一个权限(互联网)(用它更新了问题)。为什么我会得到这个?
    • @twilbrand:我不知道。你是如何发送广播的?
    • 来自我的远程服务。我有一个每 30 秒运行一次的任务。我已将代码添加到问题中。
    • @CommonsWare:太棒了,非常感谢。完美运行!
    • @H4F:我不知道你指的是什么,抱歉。我建议您就此主题提出一个单独的 Stack Overflow 问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    • 2011-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多