Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7159017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:09:40+00:00 2026-05-28T13:09:40+00:00

I have an BroadcastReceiver for handling new or modified applications: <receiver android:name=.PackageHandler > <intent-filter>

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:09:41+00:00Added an answer on May 28, 2026 at 1:09 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have my own BroadcastReceiver instance for Intent.ACTION_HEADSET_PLUG action. There is about 1-2 seconds
I've come across something like this in the AndroidManifest.xml: <receiver android:name=com.testco.test.TestReceiver/> The above is
I have the following broadcast receiver: public class MyRingModeReceiver extends BroadcastReceiver { @Override public
I have created a BroadcastReceiver . Now how can I launch my custom Intent
I have a PushNotificationReceiver (extends BroadcastReceiver) and MainActivity . The receiver sends some data
I have little concern about register BroadcastReceiver : Because Android support quite flexible, it
I have a BroadcastReceiver registered to listen for the following action.. public static final
I have an Alarm Manager which send Intent to BroadcastReceiver. This Broadcast is not
I have this Broadcast receiver registered public class NotifyAlarmBroadcast extends BroadcastReceiver{ public Context context;
I have a snippet of code that I'm calling from a service: context.registerReceiver(new BroadcastReceiver()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.