在 Android 中,如何在处理多个意图时显示单个通知?

发布于 2024-12-26 23:14:15 字数 1492 浏览 1 评论 0原文

我有一个用于处理新的或修改的应用程序的 BroadcastReceiver

<receiver android:name=".PackageHandler" >
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_ADDED" />
            <action android:name="android.intent.action.PACKAGE_CHANGED" />
            <action android:name="android.intent.action.PACKAGE_REPLACED" />
            <data android:scheme="package" />
        </intent-filter>
</receiver>

当收到意图时,我会显示一条包含一些包信息的通知。我的问题是,当我添加一个新应用程序时,例如在调试时,从 PackageHandler 收到两个意图,因此显示两个通知。为了说明这一点,这个处理讨论的意图的 BroadcastReceiver 会在每次发送意图时显示一个 logcat 条目,这在我更新应用程序时会显示两次:

public class PackageHandler extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("", "Intent Received: " + intent.getAction());
        if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
            Log.i("Intent Received", intent.getAction());
        }
        else if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) {
            Log.i("Intent Received", intent.getAction());
        }
        else if (intent.getAction().equals(Intent.ACTION_PACKAGE_CHANGED)) {
            Log.i("Intent Received", intent.getAction());
        }

    }//onReceive
}//PackageHandler

但是我需要一种跟踪已显示日志消息(来自处理不同意图)的解决方案。从通知的角度来看,我还可以选择在显示后删除额外的通知。有人有可行的解决方案吗?请注意,我支持 API 1.5。

I have an BroadcastReceiver for handling new or modified applications:

<receiver android:name=".PackageHandler" >
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_ADDED" />
            <action android:name="android.intent.action.PACKAGE_CHANGED" />
            <action android:name="android.intent.action.PACKAGE_REPLACED" />
            <data android:scheme="package" />
        </intent-filter>
</receiver>

When an intent is received, I show a notification containing some of the package information. My problem is that when I add a new app, for example while debugging, two intents are received from PackageHandler, and thus two notifications are displayed. To illustrate, this BroadcastReceiver, which handles the intents discussed, will show a logcat entry every time the intent is sent, which is twice when I update an app:

public class PackageHandler extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("", "Intent Received: " + intent.getAction());
        if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
            Log.i("Intent Received", intent.getAction());
        }
        else if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) {
            Log.i("Intent Received", intent.getAction());
        }
        else if (intent.getAction().equals(Intent.ACTION_PACKAGE_CHANGED)) {
            Log.i("Intent Received", intent.getAction());
        }

    }//onReceive
}//PackageHandler

However I need a solution that keeps track of having already showed a log message (from handling a different intents). From a notifications view point, I could also optionally remove extra notifications after they are displayed. Does anyone have a working solution? Note that I am supporting API 1.5.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白色秋天 2025-01-02 23:14:15

如果我们在 NotificationManager.notify,新通知将替换旧通知。

If we use the same unique id in NotificationManager.notify, the new notification will replace the older notification.

枕头说它不想醒 2025-01-02 23:14:15

我想出的解决方案非常有效,但我很难弄清楚。在一个包含许多静态变量(我们称之为S.java)的单独类中,我创建了一个名为pid<的整数 /em>.然后,我向 BroadcastReceiver 添加了一个 synchronized 方法,用于比较进程 PID 并决定是否匹配。大多数时候,只收到一个意图,但我最多收到两个,这可以防止这种情况。我从来没有同时对这个接收器进行过三个 intent 调用,因此它的设计目的不是为了处理这么多次。新代码(添加到上面的代码)如下:

public class PackageHandler extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (!getPID()) {
            return;
        }
        if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
            Log.i("Intent Received", intent.getAction());
        }
        else if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) {
            Log.i("Intent Received", intent.getAction());
        }
        else if (intent.getAction().equals(Intent.ACTION_PACKAGE_CHANGED)) {
            Log.i("Intent Received", intent.getAction());
        }
    }//onReceive

    public synchronized boolean getPID() {
       ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
       List<RunningAppProcessInfo> raps = am.getRunningAppProcesses();
       int pid = 0;
       for (RunningAppProcessInfo rap : raps) {
           if (rap.processName.equals("com.example.android")) {//change to your package
               pid = rap.pid;
               break;
           }
       }
       if (pid == S.pid) {
           Log.i("", "PIDs match");
           S.pid = 0;
           return false;
       }
       else {
           Log.i("", "Non-Matching PIDs");
           S.pid = pid;
           return true;
       }
   }//getPID

}//PackageHandler

The solution I came up with works really nicely, but I had a tough time figuring it out. In a separate class, which contains many static variables (let's call it S.java), I created an integer called pid. Then I added to my BroadcastReceiver to include a synchronized method that compares process PIDs and decides if one is a match. Most of the time, only one of the intents are received, however I have had up to two, which this protects against. I have never had three intent calls to this receiver at one time, so this is not designed to handle that many. The new code (adding to the above code) is as follows:

public class PackageHandler extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (!getPID()) {
            return;
        }
        if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
            Log.i("Intent Received", intent.getAction());
        }
        else if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) {
            Log.i("Intent Received", intent.getAction());
        }
        else if (intent.getAction().equals(Intent.ACTION_PACKAGE_CHANGED)) {
            Log.i("Intent Received", intent.getAction());
        }
    }//onReceive

    public synchronized boolean getPID() {
       ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
       List<RunningAppProcessInfo> raps = am.getRunningAppProcesses();
       int pid = 0;
       for (RunningAppProcessInfo rap : raps) {
           if (rap.processName.equals("com.example.android")) {//change to your package
               pid = rap.pid;
               break;
           }
       }
       if (pid == S.pid) {
           Log.i("", "PIDs match");
           S.pid = 0;
           return false;
       }
       else {
           Log.i("", "Non-Matching PIDs");
           S.pid = pid;
           return true;
       }
   }//getPID

}//PackageHandler
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文