6

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

1 回答 1

8

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

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

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

于 2010-09-17T00:13:02.477 回答